@@ -48,7 +48,7 @@ npm i @shgysk8zer0/http
4848### NPM Imports
4949``` js
5050import { HTTPError } from ' shgysk8zer0/http@shgysk8zer0/http/error.js' ;
51- import { NOT_IMPLEMENTED } from ' shgysk8zer0/http@shgysk8zer0/http/status.js' ;
51+ import { NOT_IMPLEMENTED , INTERNAL_SERVER_ERROR } from ' shgysk8zer0/http@shgysk8zer0/http/status.js' ;
5252import { JSON } from ' shgysk8zer0/http@shgysk8zer0/http/types.js' ;
5353import { Cookie } from ' shgysk8zer0/http@shgysk8zer0/http/cookie.js' ;
5454```
@@ -60,32 +60,46 @@ It is designed to be versatile and is not limited to a specific Node.js environm
6060
6161``` js
6262import { HTTPError } from ' https://unpkg.com/@shgysk8zer0/http/error.js' ;
63- import { NOT_IMPLEMENTED } from ' https://unpkg.com/@shgysk8zer0/http/status.js' ;
63+ import { NOT_IMPLEMENTED , INTERNAL_SERVER_ERROR } from ' https://unpkg.com/@shgysk8zer0/http/status.js' ;
6464import { JSON } from ' https://unpkg.com/@shgysk8zer0/http/types.js' ;
6565import { Cookie } from ' https://unpkg.com/@shgysk8zer0/http/cookie.js' ;
6666```
6767
6868### Example Code
6969
70+ ``` js
7071export async function handler () {
71- const error = new HTTPError('Not implemented.', {
72- status: NOT_IMPLEMENTED,
73- cause: new Error('I have not done this yet...'),
74- });
75-
76- return new Response([ error] , {
77- status: error.status,
78- headers: new Headers({
79- 'Content-Type': JSON,
80- 'Set-Cookie': new Cookie('uid', crypto.randomUUID(), {
81- domain: 'example.com',
82- path: '/foo',
83- maxAge: 86_400_000,
84- sameSite: 'Strict',
85- httpOnly: true,
86- partitioned: true,
87- }
88- }),
89- });
72+ try {
73+ const error = new HTTPError (' Not implemented.' , {
74+ status: NOT_IMPLEMENTED ,
75+ cause: new Error (' I have not done this yet...' ),
76+ });
77+
78+ throw err;
79+ } catch (err) {
80+ if (err instanceof HTTPError) { // Error has an HTTP status & message for use by client
81+ return Response .json (error, {
82+ status: error .status ,
83+ headers: new Headers ({
84+ ' Content-Type' : JSON ,
85+ ' Set-Cookie' : new Cookie (' uid' , crypto .randomUUID (), {
86+ domain: ' example.com' ,
87+ path: ' /foo' ,
88+ maxAge: 86_400_000 ,
89+ sameSite: ' Strict' ,
90+ httpOnly: true ,
91+ partitioned: true ,
92+ })
93+ }),
94+ });
95+ } else { // It is not an HTTPError and may contain sensitive into
96+ return Response .json ({
97+ error: {
98+ messsage: ' Something broke :(' ,
99+ status: INTERNAL_SERVER_ERROR ,
100+ }
101+ }, { status: INTERNAL_SERVER_ERROR });
102+ }
103+ }
90104}
91105```
0 commit comments