Tsed: Parsing error messages when checking parameters

Created on 3 Feb 2018  路  5Comments  路  Source: tsedio/tsed

Informations

Type |聽Version
---|---
Question | 3.9.2


Description

Hi !

I want to send a response error message in a different JSON format.
A few questions.

  1. Bad Request(400), why name is INTERNAL_SERVER_ERROR and status is 500?
  2. I want to get which parameters are missing. Is it just regular expressions?
    Example - code in BAD_REQUEST(400): Bad request, parameter request.body.code is required.

Code

@Post('')
setCode(@Required() @BodyParams('code') code: string) {...}

Error message

...
name: 'INTERNAL_SERVER_ERROR',
type: 'HTTP_EXCEPTION',
status: 500,
origin: { BAD_REQUEST(400): Bad request, parameter request.body.code is required.
...

Todos

  • [x] Example (log, usecase, etc...)
  • [x] Ts.ED version (bug/question)
bug

All 5 comments

Hi @yangukmo,

It's a regression from the previous version. I'll fix it ;)

232

@Romakita
Thank you for your quick response :)
Can I get an answer for question 2 ?
Using regular expressions to get the parameters seems too stupid...

Haaa you want to know in the response body what is the missing field...

Actually, you will received a BadRequest with a message. I can plan this feature and add a field with the missing parameter.

@yangukmo Well done.

Now RequiredParamError and ParseExpressionError have a dataPath and requestType field.

But, to return an Error as JSON, you need to implement your own GlobalErrorHandlerMiddleware.
See documentation: https://romakita.github.io/ts-express-decorators/#/docs/middlewares/global-error-middleware

Example

/**
 * @middleware
 */
@MiddlewareError()
export class GlobalErrorHandlerMiddleware implements IMiddlewareError {

    use(@Err() error: any,
        @Request() request: Express.Request,
        @Response() response: Express.Response): any {

        const toHTML = (message = "") => message.replace(/\n/gi, "<br />");

        if (error instanceof Exception || error.status) {
            request.log.error({
                error: {
                    message: error.message,
                    stack: error.stack,
                    status: error.status
                }
            });
            response.status(error.status).send(toHTML(error.message)); /// HERE
            // replace by
            response.status(error.status).json(error.message);
            return;
        }

        if (typeof error === "string") {
            response.status(404).send(toHTML(error));
            return;
        }

        request.log.error({
            error: {
                status: 500,
                message: error.message,
                stack: error.stack
            }
        });
        response.status(error.status || 500).send("Internal Error");

        return;
    }
}

See you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tobiasmuecksch picture tobiasmuecksch  路  6Comments

thanhchuongitc picture thanhchuongitc  路  6Comments

OskarLebuda picture OskarLebuda  路  4Comments

m0uneer picture m0uneer  路  7Comments

dorian-kwon picture dorian-kwon  路  3Comments