createHttpError function
Factory that maps HTTP status codes to typed error classes.
Implementation
CocobaseError createHttpError({
required int statusCode,
required String url,
required String method,
required dynamic detail,
required String suggestion,
}) {
switch (statusCode) {
case 401:
return UnauthorizedError(
url: url,
method: method,
detail: detail,
suggestion: suggestion,
);
case 403:
return ForbiddenError(
url: url,
method: method,
detail: detail,
suggestion: suggestion,
);
case 404:
return NotFoundError(
url: url,
method: method,
detail: detail,
suggestion: suggestion,
);
case 422:
return ValidationError(
url: url,
method: method,
detail: detail,
suggestion: suggestion,
);
case 429:
return RateLimitError(
url: url,
method: method,
detail: detail,
suggestion: suggestion,
);
default:
if (statusCode >= 500) {
return ServerError(
statusCode: statusCode,
url: url,
method: method,
detail: detail,
suggestion: suggestion,
);
}
return CocobaseError(
statusCode: statusCode,
url: url,
method: method,
detail: detail,
suggestion: suggestion,
);
}
}