Skip to content

Error Handling

Plenum returns structured error responses when requests fail at the gateway level. You can customise these responses with a global error interceptor.

Working example: see examples/error-handling/

StatusWhenBody
404No matching route in the spec{"error": "not found"}
405Route exists but method not defined{"error": "method not allowed"}
413Request body exceeds size limit{"error": "request body too large"}
502Upstream connection failed{"error": "upstream connection failed"}
504Request timeout exceeded{"error": "request timeout exceeded"}

The 405 response includes an Allow header listing the methods defined for that route.

Register a global error handler in x-plenum-config to customise error responses:

x-plenum-config:
on-gateway-error:
module: "./interceptors/error-handler.js"
function: onError

The interceptor receives the error details and can modify the status, headers, and body:

interceptors/error-handler.js
export function onError(input) {
return {
action: "continue",
status: input.status,
headers: {
"x-error-code": input.error.code,
},
body: {
error: input.error.code,
message: input.error.message,
},
};
}

The interceptor receives:

FieldDescription
statusThe HTTP status code (404, 413, 502, 504)
error.codeMachine-readable error code
error.messageHuman-readable description
headersRequest headers
methodHTTP method
pathRequest path