@rafikidota/http-exceptions
Version:
My own http exceptions library with blackjacks and hookers
241 lines (227 loc) • 5.6 kB
JavaScript
// src/util/status-code.ts
var HTTP_ERROR_STATUS_CODES = /* @__PURE__ */ new Set([
400,
401,
402,
403,
404,
405,
406,
407,
408,
409,
410,
411,
412,
413,
414,
415,
416,
417,
418,
421,
422,
423,
424,
425,
426,
428,
429,
431,
451,
500,
501,
502,
503,
504,
505,
506,
507,
508,
510,
511
]);
function isHttpError(status) {
return HTTP_ERROR_STATUS_CODES.has(status);
}
// src/exceptions/base-expection/http-exception.ts
var HttpException = class extends Error {
/**
* HTTP status code associated with the exception.
* @type {number}
*/
status;
/**
* Creates an instance of HttpException.
*
* @param {string} [message=Something went wrong] - Descriptive message for the exception.
* @param {number} [status=500] - HTTP status code.
*/
constructor(message = "Something went wrong", status = 500) {
if (!isHttpError(status)) {
throw new Error(`Invalid status code ${status}`);
}
super(message);
this.name = "HttpException";
this.status = status;
}
};
// src/exceptions/4xx/400-bad-request.ts
var BadRequestException = class extends HttpException {
/**
* Creates an instance of BadRequestException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 400);
this.name = "BadRequestException";
}
};
// src/exceptions/4xx/401-unauthorized.ts
var UnauthorizedException = class extends HttpException {
/**
* Creates an instance of UnauthorizedException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 401);
this.name = "UnauthorizedException";
}
};
// src/exceptions/4xx/402-payment-required.ts
var PaymentRequiredException = class extends HttpException {
/**
* Creates an instance of PaymentRequiredException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 402);
this.name = "PaymentRequiredException";
}
};
// src/exceptions/4xx/403-forbidden.ts
var ForbiddenException = class extends HttpException {
/**
* Creates an instance of ForbiddenException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 403);
this.name = "ForbiddenException";
}
};
// src/exceptions/4xx/404-not-found.ts
var NotFoundException = class extends HttpException {
/**
* Creates an instance of NotFoundException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 404);
this.name = "NotFoundException";
}
};
// src/exceptions/4xx/405-method-not-allowed.ts
var MethodNotAllowedException = class extends HttpException {
/**
* Creates an instance of MethodNotAllowedException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 405);
this.name = "MethodNotAllowedException";
}
};
// src/exceptions/4xx/415-unsupported-media-type.ts
var UnsupportedMediaTypeException = class extends HttpException {
/**
* Creates an instance of UnsupportedMediaTypeException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 415);
this.name = "UnsupportedMediaTypeException";
}
};
// src/exceptions/5xx/500-internal-server-error.ts
var InternalServerErrorException = class extends HttpException {
/**
* Creates an instance of InternalServerErrorException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 500);
this.name = "InternalServerErrorException";
}
};
// src/exceptions/5xx/501-not-implemented.ts
var NotImplementedException = class extends HttpException {
/**
* Creates an instance of NotImplementedException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 501);
this.name = "NotImplementedException";
}
};
// src/exceptions/5xx/502-bad-gateway.ts
var BadGatewayException = class extends HttpException {
/**
* Creates an instance of BadGatewayException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 502);
this.name = "BadGatewayException";
}
};
// src/exceptions/5xx/503-service-unavailable.ts
var ServiceUnavailableException = class extends HttpException {
/**
* Creates an instance of ServiceUnavailableException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 503);
this.name = "ServiceUnavailableException";
}
};
// src/exceptions/5xx/504-gateway-timeout.ts
var GatewayTimeoutException = class extends HttpException {
/**
* Creates an instance of GatewayTimeoutException.
*
* @param {string} message - A descriptive message for the exception.
*/
constructor(message) {
super(message, 504);
this.name = "GatewayTimeoutException";
}
};
export {
BadGatewayException,
BadRequestException,
ForbiddenException,
GatewayTimeoutException,
HttpException,
InternalServerErrorException,
MethodNotAllowedException,
NotFoundException,
NotImplementedException,
PaymentRequiredException,
ServiceUnavailableException,
UnauthorizedException,
UnsupportedMediaTypeException
};