@unito/integration-sdk
Version:
Integration SDK
38 lines (34 loc) • 1.62 kB
text/typescript
import * as HttpErrors from './httpErrors.js';
export class InvalidHandler extends Error {}
/**
* Processes provider response codes and returns the corresponding errors to be translated further in our responses
*
* @param responseStatus the reponseStatus of the request. Any HTTP response code passed here will result in an error!
* @param message The message returned by the provider
*/
// Keep in errors.ts instead of httpErrors.ts because we do not need to export it outside of the sdk
export function buildHttpError(responseStatus: number, message: string): HttpErrors.HttpError {
let httpError: HttpErrors.HttpError;
if (responseStatus === 400) {
httpError = new HttpErrors.BadRequestError(message);
} else if (responseStatus === 401) {
httpError = new HttpErrors.UnauthorizedError(message);
} else if (responseStatus === 403) {
httpError = new HttpErrors.ForbiddenError(message);
} else if (responseStatus === 404) {
httpError = new HttpErrors.NotFoundError(message);
} else if (responseStatus === 408) {
httpError = new HttpErrors.TimeoutError(message);
} else if (responseStatus === 410) {
httpError = new HttpErrors.ResourceGoneError(message);
} else if (responseStatus === 422) {
httpError = new HttpErrors.UnprocessableEntityError(message);
} else if (responseStatus === 423) {
httpError = new HttpErrors.ProviderInstanceLockedError(message);
} else if (responseStatus === 429) {
httpError = new HttpErrors.RateLimitExceededError(message);
} else {
httpError = new HttpErrors.HttpError(message, responseStatus);
}
return httpError;
}