0
votes

Here's a sample error message generated by Loopback for unique item:

{
    "error": {
        "statusCode": "422",
        "name": "Error",
        "message": "ER_DUP_ENTRY: Duplicate entry '[email protected]' for key 'email'",
        "code": "ER_DUP_ENTRY"
    }
}

However, I want to modify it as:

{
    "errors": {
        "email": [
            "The field email should be unique"
        ]
    }
}

Documentation really didn't help me. Can anybody help me on this please?

2

2 Answers

1
votes

You can return custom error using HttpErrors object from @loopback/rest. Please click here for more.

throw new HttpErrors.UnprocessableEntity('limit is not a natural number');
0
votes

You can call the next method with the custom error object.

let error = new Error('Custom Error message.'); //Message passed as parameter.
error.name = "UNIQUE_EMAIL";
error.status = 422; //Set status code
next(error); // call the next method with error object. 

LoopBack requires the error object to have a message, status, and the name property.

Refer to there documentation: https://loopback.io/doc/en/lb3/Error-object.html