Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Getting 500 status code when attempting to throw non 500 style errors (401) #85

Closed
camstuart opened this issue Aug 7, 2017 · 2 comments

Comments

@camstuart
Copy link

I was hoping to be able to control the HTTP status code by using feathers-errors

I have a middle-ware that places the Authorization header into the request object:

req.feathers.authorizationHeader = req.headers.authorization || ''

And then, Inside my service class method I throw a Forbidden (401) if this value is not present:

const errors = require('feathers-errors') // v2.9.1
const _ = require('underscore')

class Service {
  constructor(options) {
    this.options = options || {}
  }

  async find(params) {

    if (_.isEmpty(params.authorizationHeader)) {
      throw new errors.Forbidden('Access denied') // Was expecting a 401 response code but got 500
    }

  }
}

module.exports = function (options) {
  return new Service(options)
}

module.exports.Service = Service

The output looks like this:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>Forbidden: Access denied
            <br> &nbsp; &nbsp;at Forbidden (/srv/data/metinsight-apps/micro-service-authentication/node_modules/feathers-errors/lib/index.js:114:17)
            <br> &nbsp; &nbsp;at Object.find (/srv/data/metinsight-apps/micro-service-authentication/src/services/authenticate/authenticate.class.js:40:13)
            <br> &nbsp; &nbsp;at &lt;anonymous&gt;
        </pre>
    </body>
</html>

But the status code is 500, not 401. And I would much prefer a json object in response, not HTML

@daffl
Copy link
Member

daffl commented Aug 7, 2017

Are you using the error handler middleware and in the right place?

It will definitely set the status code accordingly if the error has one here.

@camstuart
Copy link
Author

Ahh yes indeed. the:

app.use(notFound())
app.use(handler())

Where too high up in ./src/app.js not even sure why/how that was the case.
Thankyou for your help

@daffl daffl closed this as completed Aug 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants