How to use the @bentley/bentleyjs-core.BentleyError function in @bentley/bentleyjs-core

To help you get started, we’ve selected a few @bentley/bentleyjs-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github imodeljs / imodeljs / core / clients / src / projectshare / ProjectShareClient.ts View on Github external
public async updateCustomProperties(requestContext: AuthorizedClientRequestContext, contextId: GuidString, file: ProjectShareFile, updateProperties?: Array<{ Name: string, Value: string }>, deleteProperties?: string[]): Promise {
    const customProperties: any[] = updateProperties === undefined ? new Array() : updateProperties;
    if (deleteProperties) {
      deleteProperties.forEach((value: string) => customProperties.push({
        Name: value,
        IsDeleted: true,
      }));
    }

    if (customProperties.length === 0)
      throw new BentleyError(BentleyStatus.ERROR, "No updates or deletes specified", Logger.logError, loggerCategory, () => ({ ...file }));

    const updateInstance = new ProjectShareFile();
    updateInstance.wsgId = file.wsgId;
    updateInstance.changeState = "modified"; // TBD: Not sure why we need this to be setup.
    updateInstance.customProperties = customProperties;

    const projectShareRequestOptions = {
      CustomOptions: {
        EnableAdHocChangeset: true,
      },
    };

    const relativeUrl = `/repositories/BentleyCONNECT.ProjectShareV2--${contextId}/ProjectShare/File/${file.wsgId}`;
    return this.postInstance(requestContext, ProjectShareFile, relativeUrl, updateInstance, projectShareRequestOptions);
  }
}
github imodeljs / imodeljs / core / clients-backend / src / oidc / OidcAgentClient.ts View on Github external
public get hasExpired(): boolean {
    if (!this._accessToken)
      return false;

    const expiresAt = this._accessToken.getExpiresAt();
    if (!expiresAt)
      throw new BentleyError(AuthStatus.Error, "Invalid JWT");

    return expiresAt.getTime() - Date.now() <= 1 * 60 * 1000; // Consider 1 minute before expiry as expired
  }
github imodeljs / imodeljs / plugins / geo-photo / src / JpegTagReader.ts View on Github external
const tiffOffset = start + 6;
    if (file.getUint16(tiffOffset) === 0x4949) {
      bigEnd = false;
    } else if (file.getUint16(tiffOffset) === 0x4D4D) {
      bigEnd = true;
    } else {
      throw new BentleyError(BentleyStatus.ERROR, "Not valid TIFF data! (no 0x4949 or 0x4D4D)");
    }

    if (file.getUint16(tiffOffset + 2, !bigEnd) !== 0x002A)
      throw new BentleyError(BentleyStatus.ERROR, "Not valid TIFF data! (no 0x002A)");

    const firstIFDOffset = file.getUint32(tiffOffset + 4, !bigEnd);

    if (firstIFDOffset < 0x00000008)
      throw new BentleyError(BentleyStatus.ERROR, "Not valid TIFF data! (First offset less than 8) " + file.getUint32(tiffOffset + 4, !bigEnd));

    const tags: ImageTags = this.readTagsAtOffset(file, tiffOffset, tiffOffset + firstIFDOffset, this._TiffTagDefinitions, bigEnd);

    if (tags.has("ExifIFDPointer")) {
      const exifData: ImageTags = this.readTagsAtOffset(file, tiffOffset, tiffOffset + (tags.get("ExifIFDPointer") as number), this._ExifTagDefinitions, bigEnd);
      for (const tag of exifData.keys()) {
        switch (tag) {
          case "LightSource":
          case "Flash":
          case "MeteringMode":
          case "ExposureProgram":
          case "SensingMethod":
          case "SceneCaptureType":
          case "SceneType":
          case "CustomRendered":
          case "WhiteBalance":
github imodeljs / imodeljs / core / clients-backend / src / oidc / OidcDeviceClient.ts View on Github external
private async makeRefreshTokenRequest(requestContext: ClientRequestContext): Promise {
    requestContext.enter();
    if (!this._configuration)
      throw new BentleyError(AuthStatus.Error, "Not initialized. First call initialize()", Logger.logError, loggerCategory);
    if (!this._tokenResponse)
      throw new BentleyError(AuthStatus.Error, "Missing refresh token. First call signIn() and ensure it's successful", Logger.logError, loggerCategory);

    const tokenRequestJson: TokenRequestJson = {
      grant_type: GRANT_TYPE_REFRESH_TOKEN,
      refresh_token: this._tokenResponse.refreshToken,
      redirect_uri: this._clientConfiguration.redirectUri,
      client_id: this._clientConfiguration.clientId,
    };

    const request = new TokenRequest(tokenRequestJson);
    return this._tokenHandler.performTokenRequest(this._configuration, request);
  }
github imodeljs / imodeljs / core / clients-backend / src / oidc / OidcAgentClient.ts View on Github external
private async generateAccessToken(requestContext: ClientRequestContext): Promise {
    const scope = this._configuration.scope;
    if (scope.includes("openid") || scope.includes("email") || scope.includes("profile") || scope.includes("organization"))
      throw new BentleyError(AuthStatus.Error, "Scopes for an Agent cannot include 'openid email profile organization'");

    const grantParams: GrantParams = {
      grant_type: "client_credentials",
      scope,
    };

    let tokenSet: TokenSet;
    const client = await this.getClient(requestContext);
    try {
      tokenSet = await client.grant(grantParams);
    } catch (error) {
      throw new BentleyError(AuthStatus.Error, error.message || "Authorization error", Logger.logError, loggerCategory, () => ({ error: error.error, message: error.message }));
    }
    const userInfo = OidcBackendClient.parseUserInfo(tokenSet.access_token);
    this._accessToken = this.createToken(tokenSet, userInfo);
    return this._accessToken;
github imodeljs / imodeljs / core / clients / src / Token.ts View on Github external
public toTokenString(includePrefix: IncludePrefix = IncludePrefix.Yes): string {
    if (!this.parseSamlAssertion() || !this._x509Certificate)
      throw new BentleyError(BentleyStatus.ERROR, "Invalid access token");

    const prefix = (includePrefix === IncludePrefix.Yes) ? "X509 access_token=" : "";
    return prefix + Buffer.from(this._x509Certificate, "utf8").toString("base64");
  }
}
github imodeljs / imodeljs / core / logger-config / src / FluentdBunyanLoggerConfig.ts View on Github external
public static validateProps(fluentdConfig: any): void {
    const validProps: string[] = ["host", "port"];
    for (const prop of Object.keys(fluentdConfig)) {
      if (!validProps.includes(prop)) {
        throw new BentleyError(IModelStatus.BadArg, "unrecognized fluentdConfig property: " + prop);
      }
    }
  }
}
github imodeljs / imodeljs / core / backend / src / IModelHost.ts View on Github external
public static async getAccessToken(requestContext: ClientRequestContext = new BackendRequestContext()): Promise {
    requestContext.enter();
    if (!this.authorizationClient)
      throw new BentleyError(AuthStatus.Error, "No AuthorizationClient has been supplied to IModelHost", Logger.logError, loggerCategory);
    if (!this.authorizationClient.hasSignedIn)
      throw new BentleyError(AuthStatus.Error, "AuthorizationClient has not been used to sign in", Logger.logError, loggerCategory);
    return this.authorizationClient.getAccessToken(requestContext);
  }