The new decorator function.
import { BodyParseErrorHandler, Controller, ControllerBase, IHttpRequest, IHttpResponse, ParseError, ParseErrorHandler, POST, schema } from '@egomobile/http-server'
const mySchema = schema.object({
email: schema.string().trim().email().required(),
name: schema.string().trim().min(1).optional()
})
@Controller()
export default class MyController extends ControllerBase {
@POST(mySchema)
async index(request: IHttpRequest, response: IHttpResponse) {
response.write('You send: ' + JSON.stringify(response.body!))
}
@BodyParseErrorHandler() // mark that method as default
// parse error handler inside that controller
async handleBodyParseError(error: ParseError, request: IHttpRequest, response: IHttpResponse) {
const errorMessage = Buffer.from('PARSE ERROR: ' + error.innerError?.message, 'utf8')
response.writeHead(400, {
'Content-Length': String(errorMessage.length)
})
response.write(errorMessage)
response.end()
}
}
Generated using TypeDoc
Add a method of a controller as a parse error handler.