http-exception.js 957 B

1234567891011121314151617181920212223242526272829303132333435
  1. // src/http-exception.ts
  2. var HTTPException = class extends Error {
  3. res;
  4. status;
  5. /**
  6. * Creates an instance of `HTTPException`.
  7. * @param status - HTTP status code for the exception. Defaults to 500.
  8. * @param options - Additional options for the exception.
  9. */
  10. constructor(status = 500, options) {
  11. super(options?.message, { cause: options?.cause });
  12. this.res = options?.res;
  13. this.status = status;
  14. }
  15. /**
  16. * Returns the response object associated with the exception.
  17. * If a response object is not provided, a new response is created with the error message and status code.
  18. * @returns The response object.
  19. */
  20. getResponse() {
  21. if (this.res) {
  22. const newResponse = new Response(this.res.body, {
  23. status: this.status,
  24. headers: this.res.headers
  25. });
  26. return newResponse;
  27. }
  28. return new Response(this.message, {
  29. status: this.status
  30. });
  31. }
  32. };
  33. export {
  34. HTTPException
  35. };