Errors
Return an http error
Bridge provides a method to return errors to the client called httpError
.
For example, this endpoint:
server.tsts
import {handler ,httpError } from 'bridge';consthello =handler ({resolve : () =>httpError (400, 'Expired Session'),});
server.tsts
import {handler ,httpError } from 'bridge';consthello =handler ({resolve : () =>httpError (400, 'Expired Session'),});
Results to the following response with an error status 400:
json
{"error": {"status": 400,"name": "Expired Session"}}
json
{"error": {"status": 400,"name": "Expired Session"}}
The StatusCode
enum in the Bridge library can be used to easily specify an error status in your response. You can also include additional details by passing them as the third argument to httpError
, which will be sent to the client.
For example, this endpoint:
server.tsts
import {handler ,httpError ,StatusCode } from 'bridge';consthello =handler ({resolve : () =>httpError (StatusCode .UNAUTHORIZED , 'Expired Session', {reason : 'example' }),});
server.tsts
import {handler ,httpError ,StatusCode } from 'bridge';consthello =handler ({resolve : () =>httpError (StatusCode .UNAUTHORIZED , 'Expired Session', {reason : 'example' }),});
Results to the following response with an error status 400:
json
{"error": {"status": 400,"name": "Expired Session","data": {"reason": "example"}}}
json
{"error": {"status": 400,"name": "Expired Session","data": {"reason": "example"}}}
Throw an error
If you throw an error within a handler, it will send a JSON response to the client with a status code of 500. Similarly, an unexpected error in the resolve function of a handler will also result in a 500 response to the client. The client will not receive information about the cause of the error, which can be handled in Bridge's errorHandler
function.
For example, this endpoint:
server.tsts
import {handler } from 'bridge';consthello =handler ({resolve : () => {throw newError ('Test error');},});
server.tsts
import {handler } from 'bridge';consthello =handler ({resolve : () => {throw newError ('Test error');},});
Results to the following response with an error status 500:
json
{"error": {"status": 500,"name": "Internal server error"}}
json
{"error": {"status": 500,"name": "Internal server error"}}
Error Handler
All errors that occur in a handler are passed through the onError
method before being sent to the client. This gives you the ability to customize the error object, log the errors, or send them to a bug reporting platform like Sentry.
Example
ts
import {initBridge ,onError } from 'bridge';consterrorHandler =onError (({error ,path }) => {// The error object can be modified here before it is sent to the clientif (error .name === 'Internal server error')console .log (path ,error ); // Send to bug reportingelseconsole .log (path ,error .status ,error .name ,error .data );});constbridge =initBridge ({routes : {},errorHandler });
ts
import {initBridge ,onError } from 'bridge';consterrorHandler =onError (({error ,path }) => {// The error object can be modified here before it is sent to the clientif (error .name === 'Internal server error')console .log (path ,error ); // Send to bug reportingelseconsole .log (path ,error .status ,error .name ,error .data );});constbridge =initBridge ({routes : {},errorHandler });
The onError parameter is an object that contains all information about the error and the context it occurs in:
ts
{path: string;error: {name: string;status: number;data?: any;}}
ts
{path: string;error: {name: string;status: number;data?: any;}}