@@ -30,38 +30,184 @@ pnpm add error-lib
3030To use any of the custom error libraries you need to simply import them in your typescript/javascript application.
3131
3232``` js
33-
3433// for NodeJS applications (Common JS)
35- const { ApplicationError , NotFoundError } = require (' error-lib' );
34+ const {
35+ ApplicationError ,
36+ NotFoundError ,
37+ ForbiddenError ,
38+ } = require (' error-lib' );
39+
40+ // Let's suppose we have a snippet that reads the content of a file
41+ // 1) The first step is to check if the file exists.
42+ // 2) The second step is to check if current user has access to the file.
43+
44+ const checkIfFileExist = (path ) => {
45+ // 'fs.exists' is a pseudo code
46+ if (fs .exists (path) === false ) {
47+ throw new NotFoundError (` ${ path} was not found.` );
48+ }
3649
37- try {
50+ return true ;
51+ };
3852
39- if (fileNotFound === true ) {
40- throw new NotFoundError (' config.json was not found' );
53+ const readFileContent = (path ) => {
54+ if (fs .hasAccess (path) === false ) {
55+ throw new ForbiddenError (` User does not have access to '${ path} '` );
4156 }
42- }
43- catch (err) {
4457
58+ return ' dummy content' ;
59+ };
60+
61+ try {
62+ // step 1
63+ checkIfFileExist (' /path/to/file' );
64+
65+ // step 2
66+ const fileContent = readFileContent (' /path/to/file' );
67+ } catch (err) {
4568 if (err instanceof NotFoundError) {
46- //
69+ // now you have intellisense enabled
70+ console .error (' File not found!' );
71+ } else if (err instanceof ForbiddenError) {
72+ // now you have intellisense enabled
73+ console .error (' No access to the file' );
74+ } else {
75+ console .error (' Something went wrong!' );
4776 }
4877}
49-
5078```
5179
5280``` ts
53-
5481// For typescript/javascript (ES Module)
5582import { ApplicationError , NotFoundError } from ' error-lib' ;
5683
84+ // Let's suppose we have a snippet that reads the content of a file
85+ // 1) The first step is to check if the file exists.
86+ // 2) The second step is to check if current user has access to the file.
5787
88+ const checkIfFileExist = (path ) => {
89+ // 'fs.exists' is a pseudo code
90+ if (fs .exists (path ) === false ) {
91+ throw new NotFoundError (` ${path } was not found. ` );
92+ }
93+
94+ return true ;
95+ };
5896
97+ const readFileContent = (path ) => {
98+ if (fs .hasAccess (path ) === false ) {
99+ throw new ForbiddenError (` User does not have access to '${path }' ` );
100+ }
59101
102+ return ' dummy content' ;
103+ };
104+
105+ try {
106+ // step 1
107+ checkIfFileExist (' /path/to/file' );
108+
109+ // step 2
110+ const fileContent = readFileContent (' /path/to/file' );
111+ } catch (err ) {
112+ if (err instanceof NotFoundError ) {
113+ // now you have intellisense enabled
114+ console .error (' File not found!' );
115+ } else if (err instanceof ForbiddenError ) {
116+ // now you have intellisense enabled
117+ console .error (' No access to the file' );
118+ } else {
119+ console .error (' Something went wrong!' );
120+ }
121+ }
60122```
61123
62124## Extend / Custom errors
63125
126+ Not the errors created in this package supports all the scenarios. It's not even possible 😁.
64127
128+ To add a new type of error that suits your needs, follow the instruction below.
129+
130+ > It's always a good idea to extend errors from one of the main error types in this package. Unless you have your own reasons not to do so 😁.
131+
132+ ``` js
133+ // Let's suppose you're adding an InvalidUsernamePassword error (which can be derived from BadRequestError).
134+
135+ // invalid_username_password_error.ts
136+ const { BadRequestError } = require (' error-lib' );
137+
138+ class InvalidUsernamePassword extends BadRequestError {
139+ /**
140+ *
141+ * @param message {string} Custom error message
142+ * @param opts Extra options
143+ */
144+ constructor (message , opts ) {
145+ message = message ?? ' InvalidUsernamePasswordError' ;
146+
147+ super (message, {
148+ cause: opts? .cause ,
149+ code: opts? .code ?? ' E_INVALID_USERNAME_PASSWORD' ,
150+ });
151+
152+ Error .captureStackTrace (this , InvalidUsernamePassword);
153+ Object .setPrototypeOf (this , InvalidUsernamePassword .prototype );
154+ }
155+ }
156+
157+ module .exports = {
158+ InvalidUsernamePassword,
159+ };
160+
161+ // in your application (e.g. app.js)
162+ // Now you can use your new error class to throw more specific errors
163+
164+ if (user !== ' user1' && pass !== ' p4$sw0rd!' ) {
165+ throw new InvalidUsernamePassword ();
166+ }
167+ ` ` `
168+
169+ ` ` ` ts
170+ // Let's suppose you're adding an InvalidUsernamePassword error (which can be derived from BadRequestError).
171+
172+ // invalid_username_password_error.ts
173+
174+ import { BadRequestError , BadRequestErrorConstructorOptions } from ' error-lib' ;
175+
176+ export interface InvalidUsernamePasswordConstructorOptions <
177+ TCauseError extends Error = Error ,
178+ > extends BadRequestErrorConstructorOptions< TCauseError> {}
179+
180+ export class InvalidUsernamePassword <
181+ TCause extends Error = Error ,
182+ > extends BadRequestError< TCause> {
183+ /**
184+ *
185+ * @param message Custom error message
186+ * @param opts Extra options
187+ */
188+ constructor (
189+ message ?: string ,
190+ opts ?: InvalidUsernamePasswordConstructorOptions < TCause > ,
191+ ) {
192+ message = message ?? ' InvalidUsernamePasswordError' ;
193+
194+ super (message, {
195+ cause: opts? .cause ,
196+ code: opts? .code ?? ' E_INVALID_USERNAME_PASSWORD' ,
197+ });
198+
199+ Error .captureStackTrace (this , InvalidUsernamePassword);
200+ Object .setPrototypeOf (this , InvalidUsernamePassword .prototype );
201+ }
202+ }
203+
204+ // in your application (e.g. app.ts)
205+ // Now you can use your new error class to throw more specific errors
206+
207+ if (user !== ' user1' && pass !== ' p4$sw0rd!' ) {
208+ throw new InvalidUsernamePassword ();
209+ }
210+ ` ` `
65211
66212And you're good to go!
67213
0 commit comments