mulesoft-labs / osprey-method-handler

Middleware for validating requests and responses based on a RAML method object
Other
16 stars 16 forks source link

UnsupportedMediaTypeError handling #15

Closed pickworth closed 8 years ago

pickworth commented 8 years ago

UnsupportedMediaTypeError: No body sent with request for PUT /api/something with content-type "undefined"

Seems that there is no way to handle such error ?

err object appears to be empty for this case.

Please advise

Thanks

blakeembrey commented 8 years ago

What's the expected behaviour? Can you provide a small replication to demonstrate the issue?

blakeembrey commented 8 years ago

The RAML document, I assume, defines that you need a body and the application has not sent a body with the HTTP request.

pickworth commented 8 years ago

it's okay, i guess i can use something like:

if(err.name == "UnsupportedMediaTypeError" )){
   res.status(415);
....
}

I was just expecting err.code to have 415 in it, instead it is undefined err.type is undefined also

pickworth commented 8 years ago

Probably this isn't a real a bug, sorry

blakeembrey commented 8 years ago

@nmors Now I understand, cheers! It is created with 415, it has a status property which means default Express.js error handlers will use the correct HTTP status code: https://github.com/mulesoft-labs/osprey-method-handler/blob/master/osprey-method-handler.js#L306.

blakeembrey commented 8 years ago

Let me know if the status property helps you, it's the "standard" way in Express.js to communicate the HTTP status code (E.g. https://github.com/jshttp/http-errors/blob/master/index.js#L97).

pickworth commented 8 years ago

yep! checking error.status now for handling my errors. thanks for your help

blakeembrey commented 8 years ago

No problem, closing it resolved :+1: It sounds like you may have been trying to do something else, just keep in mind other middleware can respond with a status error of 415 too (since it's just a HTTP code).

jansanchez commented 8 years ago

Hi @nmors I've the same problem. How did you solve? pls