Star

Created With

linkError Handling



When an endpoint fails, conventionally the endpoint should return a not-ok status code, based on what went wrong. You can do the same thing in TyFON by throwing errors that also have a status property. It is recommended to use a module such as http-errors for that purpose:

1link$npm i http-errors

2link$npm i @types/http-errors --save-dev

1linkimport errors from 'http-errors';

2link

3linkexport function myRemoteFunc(...) {

4link ...

5link

6link if (/* the user does not have access*/) {

7link throw new errors.Forbidden('This operation is not allowed!');

8link } else if (/* the thing is not found */) {

9link throw new errors.NotFound('Could not find that thing');

10link }

11link

12link ...

13link}


👉 When the thrown error object has a status code, TyFON will use that status code for the response. So in this example, if the first condition holds, the response will be 403, if the second condition holds, the response will be 404, etc.


👉 Also, if the thrown error has expose property set on it to false, then TyFON will not include the error message in the response. For http-errors, expose property is by default set to false for any error with status code >= 500.



Error Handling

Home Conventions Error Handling

CLI Referencechevron_right
Advanced Usagechevron_right