Thursday, October 19, 2017

Why is Rails using the default debug_exception_response_format in test mode?

Leave a Comment

Given that I've set the following in config/environments/test.rb (I know I don't need to, but I just want to be certain):

config.debug_exception_response_format = :api 

Why are exceptions triggered by my Cucumber features coming back as HTML?

When the admin attempts to create a new vendor # features/step_definitions/vendor_steps.rb:9                                                                 743: unexpected token at '<!DOCTYPE html>                                                                                                                  <html lang="en">                                                                                                                                           <head>                                                                                                                                                       <meta charset="utf-8" />                                                                                                                                   <title>Action Controller: Exception caught</title>                                                                                                         <style>                                                                                                                                                      body {                                                                                                                                                       background-color: #FAFAFA;                                                                                                                                 color: #333;                                                                                                                                               margin: 0px;                                                                                                                                             }                                                                        

Shouldn't I be getting JSON back in this case or am I misunderstanding something?

EDIT:

Per some of the comments below, I've verified that the Content-Type is application/json and that config.debug_exception_response_format is configured correctly in development and staging environments. Unfortunately, I'm still seeing this issue.

From within the affected controller:

(byebug) request.headers["Content-Type"]
"application/json"

2 Answers

Answers 1

config.debug_exception_response_format = :api

means

To render debugging information preserving the response format, use the value :api.

Rails doc

Given this is a feature spec for admin, therefore the request is very likely to be a HTML request, so the response will also be in HTML format.

Edit

Even your request format is explicitly set as json, you still need to explicitly define the response format to be json. Because without defining the response format explicitly something like the following, it would still be in HTML format regardless the request type.

respond_to do |format|   format.html ...   format.json ... end 

or

render json: {hello: 'world'} 

Answers 2

You need to set this inside of your config/environments/development.rb as well.

config.debug_exception_response_format = :api 

Even if you are running your application in api mode, if your application was created as a default rails application then your development exception response will run in the default format response of rendering HTML/XHR.

EDIT:

Can you try adding the following test helpers?

def api_get action, params={}, version="1"   get "/api/v#{version}/#{action}", params   JSON.parse(response.body) rescue {} end  def api_post action, params={}, version="1"   post "/api/v#{version}/#{action}", params   JSON.parse(response.body) rescue {} end  def api_delete action, params={}, version="1"   delete "/api/v#{version}/#{action}", params   JSON.parse(response.body) rescue {} end  def api_put action, params={}, version="1"   put "/api/v#{version}/#{action}", params   JSON.parse(response.body) rescue {} end 

Did you take the byebug breakpoint out to ensure that it doesn't have anything to do with that gem?

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment