@unito/integration-sdk
Version:
Integration SDK
98 lines (97 loc) • 3.34 kB
JavaScript
/**
* Error class meant to be returned by integrations in case of exceptions. These errors will be caught and handled
* appropriately. Any other error would result in an unhandled server error accompanied by a 500 status code.
*
* @field message - The error message
* @field status - The HTTP status code to return
* @field retryAfter - The minimum number of seconds to wait before retrying the request.
* @field responseHeaders - The raw response headers from the provider, when the error came from a provider response.
*/
export class HttpError extends Error {
status;
// Not readonly: the SDK populates these centrally in Provider.handleError after the error is built/returned.
retryAfter = undefined;
responseHeaders = undefined;
constructor(message, status, options = {}) {
super(message);
this.status = status;
this.name = this.constructor.name;
this.retryAfter = options.retryAfter;
this.responseHeaders = options.responseHeaders;
}
}
/**
* Used to generate a 400 Bad Request. Usually used when something is missing to properly handle the request.
*/
export class BadRequestError extends HttpError {
constructor(message) {
super(message || 'Bad request', 400);
}
}
/**
* Used to generate a 401 Unauthorized. Usually used when the credentials are missing or invalid.
*/
export class UnauthorizedError extends HttpError {
constructor(message) {
super(message || 'Unauthorized', 401);
}
}
/**
* Used to generate a 403 Forbidden. Usually used when user lacks sufficient permission to access a ressource.
*/
export class ForbiddenError extends HttpError {
constructor(message) {
super(message || 'Forbidden', 403);
}
}
/**
* Used to generate a 404 Not Found. Usually used when the requested `Item` is not found.
*/
export class NotFoundError extends HttpError {
constructor(message) {
super(message || 'Not found', 404);
}
}
/**
* Used to generate a 408 Timeout Error. Usually used when the call length exceeds the received Operation Deadline.
*/
export class TimeoutError extends HttpError {
constructor(message) {
super(message || 'Not found', 408);
}
}
/**
* Used to generate a 410 Resource Gone.
*
* @deprecated Use ProviderInstanceLockedError instead when the provider instance is locked or unavailable.
*/
export class ResourceGoneError extends HttpError {
constructor(message) {
super(message || 'Resource Gone', 410);
}
}
/**
* Used to generate a 422 Unprocessable Entity. Usually used when an operation is invalid.
*/
export class UnprocessableEntityError extends HttpError {
constructor(message) {
super(message || 'Unprocessable Entity', 422);
}
}
/**
* Used to generate a 423 Provider Instance Locked.
*/
export class ProviderInstanceLockedError extends HttpError {
constructor(message, options = {}) {
super(message || 'Provider instance locked or unavailable', 423, options);
}
}
/**
* Used to generate a 429 Rate Limit Exceeded. Usually used when an operation triggers or would trigger a rate limit
* error on the provider's side.
*/
export class RateLimitExceededError extends HttpError {
constructor(message, options = {}) {
super(message || 'Rate Limit Exceeded', 429, options);
}
}