Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Creating new methods to validate the new errors is good because it will allow the validation of specific attributes (for example: err.extensions?.rbiErrorDomain) without needing to use the feature flag or the optional creation.

How?

The new interface IRbiError and new methods will be parseGraphQLRBIError and parseGraphQLRBIError:

IRbiError
Expand
titleIRbiError interface details
Code Block
languagetypescript
export interface IRbiError {
  errorCode: string;
  rbiErrorCode: string;
  rbiErrorDomain: string;
  statusCode: string;
  message: string;
}
mapRBIErrors
  • path: intl-whitelabel-app/workspaces/frontend/src/utils/errors/index.ts

Expand
titlemapRBIErrors details
Code Block
/**
 * Maps GraphQL errors to a specific RbiError2IRbiError format.
 *
 * This function filters and transforms GraphQL errors based on the presence
 * of the 'rbiErrorDomain' field in the error's extensions. If the 'rbiErrorDomain'
 * is present, the error is transformed into the RbiError2IRbiError format
 *
 * @param err - The GraphQL error object to be transformed.
 * @returns A transformed error object in RbiError2IRbiError format if applicable, otherwise undefined.
 */
const mapRBIErrors = filterMap<GQLError, NonNullableObject<GQLError>, RbiError2>IRbiError>(
  err => !!err.extensions?.rbiErrorDomain,
  err => {
    const { code, rbiErrorCode, rbiErrorDomain, statusCode } = err.extensions || {};
    return {
      errorCode: code,
      rbiErrorCode,
      rbiErrorDomain,
      statusCode,
      message: err?.message,
    } as RbiError2IRbiError;
  }
);
parseGraphQLRBIError

...

Expand
titleparseGraphQLRBIError details
Code Block
languagetypescript
/**
 * Parses a GraphQL error, returning an array of RbiError2IRbiError objects.
 * If the error is an ApolloError, it uses the graphQLErrors; otherwise, it falls back on originalError.
 *
 * @param error - The GraphQL error to parse.
 * @returns An array of RbiError2IRbiError objects.
 */
export function parseGraphQLRBIError(error: GraphQLError | ApolloError): RbiError2IRbiError[] {
  if (isApolloError(error)) {
    return mapRBIErrors(error.graphQLErrors);
  }
  return mapRBIErrors(error.originalError || []);
}

...

Expand
titlemapRBIErrors details
Code Block
languagetypescript
const mapRBIErrors = filterMap<GQLError, NonNullableObject<GQLError>, RbiError2>IRbiError>(
  err => _FEATURE_FLAG_VALUE_ && !!err.extensions?.rbiErrorDomain,
  err => {
    const { code, rbiErrorCode, rbiErrorDomain, statusCode } = err.extensions || {};
    return {
      errorCode: code,
      rbiErrorCode,
      rbiErrorDomain,
      statusCode,
      message: err?.message,
    } as RbiError2IRbiError;
  }
);

In addition of creating the new methods mapRBIErrors and parseGraphQLRBIError, it will need to create the other methods:

...