New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text template for source code #22172

Merged
merged 1 commit into from Nov 24, 2015

Conversation

Projects
None yet
2 participants
@tijmenb
Contributor

tijmenb commented Nov 3, 2015

When a request is made with AJAX and an error occurs, Rails will render a text-template for the exception instead of the HTML error page (#11960).

The .text.erb variant of the _source template is currently missing, causing HTML to be rendered in the response. This commit adds the text template.

Related to #14745.

Before

~/testing-exceptions  ᐅ curl 'http://localhost:3000/' -H 'X-Requested-With: XMLHttpRequest'
RuntimeError in PostsController#index

    <div class="source " id="frame-source-0">
      <div class="info">
        Extracted source (around line <strong>#3</strong>):
      </div>
      <div class="data">
        <table cellpadding="0" cellspacing="0" class="lines">
          <tr>
...

After

~/testing-exceptions  ᐅ curl 'http://localhost:3000/' -H 'X-Requested-With: XMLHttpRequest'
RuntimeError in PostsController#index

Extracted source (around line #3):

#1 class PostsController < ApplicationController
#2   def index
*3     raise
#4   end
#5 end
...

@rafaelfranca rafaelfranca self-assigned this Nov 3, 2015

Add text template for source code
When a request is made with AJAX and an error occurs, Rails will render
a text-template for the exception instead of the HTML error page
(#11960).

The `.text.erb` variant of the `_source` template is currently missing,
causing HTML to be rendered in the response. This commit adds the text
template.

To keep the page scannable we only only show the first three source
extracts.

Related to #14745.

Before:

```
~/testing-exceptions  ᐅ curl 'http://localhost:3000/' -H
'X-Requested-With: XMLHttpRequest'
RuntimeError in PostsController#index

    <div class="source " id="frame-source-0">
      <div class="info">
        Extracted source (around line <strong>#3</strong>):
      </div>
      <div class="data">
        <table cellpadding="0" cellspacing="0" class="lines">
          <tr>
```

After:

```
~/testing-exceptions  ᐅ curl 'http://localhost:3000/' -H
'X-Requested-With: XMLHttpRequest'
RuntimeError in PostsController#index

Extracted source (around line #3):

*3     raise
```

@tijmenb tijmenb force-pushed the tijmenb:fix-source-in-show-exception branch to 08689a2 Nov 3, 2015

tijmenb added a commit to alphagov/publishing-api that referenced this pull request Nov 5, 2015

Force text error template when exceptions occur
Rails by default shows nice error pages, with a lot of markup, css and
javascript. This is cool when you visit the page in the browser, but
not helpful when you consume the app as an API. Rails' HTML response
then makes it really hard find the error when it's displayed as part of
the exception message.

But Rails will render a text-based exception when doing AJAX requests.
By setting the `X-Requested-With` header we can make Rails show the
text template for all requests.

Note that there's still some markup in the error. This is a Rails bug,
addressed in rails/rails#22172.

I feel this is not a great solution. I'm planning on making a PR on
Rails to show the text-template for all non-HTML requests.

tijmenb added a commit to alphagov/publishing-api that referenced this pull request Nov 5, 2015

Force text error template when exceptions occur
Rails by default shows nice error pages, with a lot of markup, css and
javascript. This is cool when you visit the page in the browser, but
not helpful when you consume the app as an API. Rails' HTML response
then makes it really hard find the error when it's displayed as part of
the exception message.

But Rails will render a text-based exception when doing AJAX requests.
By setting the `X-Requested-With` header we can make Rails show the
text template for all requests.

Note that there's still some markup in the error. This is a Rails bug,
addressed in rails/rails#22172.

I feel this is not a great solution. I'm considering a PR on Rails to
show the text-template for all non-HTML requests, or make it
configurable.

rafaelfranca added a commit that referenced this pull request Nov 24, 2015

@rafaelfranca rafaelfranca merged commit 40acdcf into rails:master Nov 24, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment