http-exception.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var http_exception_exports = {};
  19. __export(http_exception_exports, {
  20. HTTPException: () => HTTPException
  21. });
  22. module.exports = __toCommonJS(http_exception_exports);
  23. class HTTPException extends Error {
  24. res;
  25. status;
  26. /**
  27. * Creates an instance of `HTTPException`.
  28. * @param status - HTTP status code for the exception. Defaults to 500.
  29. * @param options - Additional options for the exception.
  30. */
  31. constructor(status = 500, options) {
  32. super(options?.message, { cause: options?.cause });
  33. this.res = options?.res;
  34. this.status = status;
  35. }
  36. /**
  37. * Returns the response object associated with the exception.
  38. * If a response object is not provided, a new response is created with the error message and status code.
  39. * @returns The response object.
  40. */
  41. getResponse() {
  42. if (this.res) {
  43. const newResponse = new Response(this.res.body, {
  44. status: this.status,
  45. headers: this.res.headers
  46. });
  47. return newResponse;
  48. }
  49. return new Response(this.message, {
  50. status: this.status
  51. });
  52. }
  53. }
  54. // Annotate the CommonJS export names for ESM import in node:
  55. 0 && (module.exports = {
  56. HTTPException
  57. });