Loopback: Cannot change error message if status 500 in Loopback 3.0

Created on 29 Sep 2016  路  6Comments  路  Source: strongloop/loopback

Hi
Since I use loopback 3.0, I've a strange issue concerning return errors.
Since loopback 2, I use promises instead of callbacks and that is great.
A very simple example, working on loopback 2:

mymodel.getByName = function(name) {
   return new Promise(function(resolve, reject) {
      mymodel.find({"where": {"and": "ThisSyntaxIsNotCorrect"}})
      .then(resolve)
      .catch(function(error) {
         reject({
            "message": "There was an error in your request!",
            "status": 500
         });
      }
   });
}

Now I run it in loopback 3.0.0; if I set a reject status different from 500 (e.g. 404, 400, ...), I got my custom message. But if I don't specify a status or specify status 500, my message is always erased by the default message "Internal Server Error".

Thanks in advance.

doc stale

Most helpful comment

I went through this error myself and what worked for me was to add this in safeFields:

    "strong-error-handler": {
      "params": {
        "safeFields": [
          "code",
          "message"
        ]
      }
    }

All 6 comments

@pruje this is intentional. When running in production mode, we are hiding 5xx error messages because they can leak sensitive information (e.g. file paths in fs errors). See http://loopback.io/doc/en/lb3/3.0-Release-Notes.html#new-error-handler-for-rest-adapter

For debugging purposes during development, you can enable debug mode - see https://github.com/strongloop/loopback/blob/72b57ca68dee6f8b2042abf6288cb41e4752fc4c/3.0-RELEASE-NOTES.md#remove-loopbackerrorhandler.

@crandmck it makes me wonder, is this something to improve in our documentation?

@bajtos thanks for the information. If you can improve documentation, it would be great.
For me it's OK; I let you close this issue if you want.

Yes, in general we need to go through the release notes and propagate the information to the rest of the docs as appropriate.

For this issue specidfically, I think we could mention this in Using strong-error-handler and perhaps Environment-specific configuration.

Does that sound right? Anywhere else?

in Migrating to 3.0 note, it is stated that one shall set in config.json

{
  ...
  "remoting": {
    "errorHandler": {
      "debug": true,
      "log": false
    }
  }
}

and in middleware.json

{
  "final:after": {
    "strong-error-handler": {
      "params": {
         "debug": false,
         "log": true,
       }
    }
  }
}

but i can't find any impact of the latest on the overall app behavior. Removing the config lines in middleware.json does work the exact same way. I'm confused about the requirement to have this config in middleware.json.
(maybe something to do with https://github.com/strongloop/loopback/issues/1762?)


also if i programmatically specify the config.json file to use, using NODE_ENV var, i seem to have a conflict when using the config file name config.development.json : the params from this file overrides the params from config.json even when not setting NODE_ENV=development. If i change for config.dev.json it's ok.
Is this a reserved file name?

same in #2805

for me even the errors are not printed in JSON format.
in config.json

"remoting":{...
    "errorHandler": {
      "debug": true
    }
}

suppresses however the stack trace

I went through this error myself and what worked for me was to add this in safeFields:

    "strong-error-handler": {
      "params": {
        "safeFields": [
          "code",
          "message"
        ]
      }
    }
Was this page helpful?
0 / 5 - 0 ratings