ServiceError.fromCode constructor

ServiceError.fromCode(
  1. ServiceErrorCode code
)

Creates a ServiceError from a ServiceErrorCode with a default message.

This factory provides standard error messages for common error codes.

code The error code to create a ServiceError for.

Returns a ServiceError with an appropriate default message.

Example

final error = ServiceError.fromCode(ServiceErrorCode.notFound);
print(error.message); // "Not found"

Implementation

factory ServiceError.fromCode(ServiceErrorCode code) {
  switch (code) {
    case ServiceErrorCode.notFound:
      return ServiceError('Not found', code);
    case ServiceErrorCode.unauthorized:
      return ServiceError('Unauthorized', code);
    case ServiceErrorCode.badRequest:
      return ServiceError('Bad request', code);
    case ServiceErrorCode.forbidden:
      return ServiceError('Forbidden', code);
    case ServiceErrorCode.tooManyRequests:
      return ServiceError('Too many requests', code);
    case ServiceErrorCode.internal:
    default:
      return ServiceError('Internal error', code);
  }
}