<commit_before># RescueFrom

This is the gem that brings together disparate systems within OpenStax and abstracts consistent exception rescuing with html and json responses, and email notifying

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'rescue_from', '~> 1.0.0'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install rescue_from

## Usage

Just declare the openstax exception rescuer in your controller, preferably your `ApplicationController`

```ruby
class ApplicationController < ActionController::Base
  # ...

  openstax_exception_rescue

  # ...
end
```

## How it works

OpenStax::RescueFrom uses a `WrappedException` around the rescued exception for convenient access to information such as: `name`, `header`, `status`, `message`, and `error_id`

From there, an `OpenStax::RescueFrom::Logger` is used to write customized entries to the configured `system_logger` and to do recursive logging for exceptions with causes

## Configuration

This configuration, which you should place in `./config/initializers/openstax_rescue_from.rb` shows the defaults

```ruby
OpenStax::RescueFrom.configure do |config|
  config.raise_exceptions = false          # Rails users may wish to use Rails.application.config.consider_all_requests_local to decide this
  config.system_logger = Rails.logger      # Any logger that takes a string in an #error method will work
  config.html_template_path = 'errors/any' # The template path for the HTML response
  config.layout_name = 'application'       # The layout name for the HTML response
end
```

## HTML Response

Renders the `errors/any` template in `./app/views` in the `application` layout. Client must provide this. Install generator possibly coming soon to copy a stock one in.

Returns corresponding HTTP status code

## JSON Response

```json
{ error_id: 'some_generated_error_id_for_log_reference' }
// with the corresponding HTTP status code
```

## All other formats

Renders a blank response body with the corresponding HTTP status code

## HTTP Status Codes

See `OpenStax::RescueFrom::WrappedException::STATUS_MAP`

Located as of this writing, https://github.com/openstax/rescue_from/blob/implement-exception-emails/lib/openstax/rescue_from/wrapped_exception.rb#L74
<commit_msg>Fix JSON example<commit_after># RescueFrom

This is the gem that brings together disparate systems within OpenStax and abstracts consistent exception rescuing with html and json responses, and email notifying

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'rescue_from', '~> 1.0.0'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install rescue_from

## Usage

Just declare the openstax exception rescuer in your controller, preferably your `ApplicationController`

```ruby
class ApplicationController < ActionController::Base
  # ...

  openstax_exception_rescue

  # ...
end
```

## How it works

OpenStax::RescueFrom uses a `WrappedException` around the rescued exception for convenient access to information such as: `name`, `header`, `status`, `message`, and `error_id`

From there, an `OpenStax::RescueFrom::Logger` is used to write customized entries to the configured `system_logger` and to do recursive logging for exceptions with causes

## Configuration

This configuration, which you should place in `./config/initializers/openstax_rescue_from.rb` shows the defaults

```ruby
OpenStax::RescueFrom.configure do |config|
  config.raise_exceptions = false          # Rails users may wish to use Rails.application.config.consider_all_requests_local to decide this
  config.system_logger = Rails.logger      # Any logger that takes a string in an #error method will work
  config.html_template_path = 'errors/any' # The template path for the HTML response
  config.layout_name = 'application'       # The layout name for the HTML response
end
```

## HTML Response

Renders the `errors/any` template in `./app/views` in the `application` layout. Client must provide this. Install generator possibly coming soon to copy a stock one in.

Returns corresponding HTTP status code

## JSON Response

```json
{ "error_id" : "some_generated_error_id_for_log_reference" }
// with the corresponding HTTP status code
```

## All other formats

Renders a blank response body with the corresponding HTTP status code

## HTTP Status Codes

See `OpenStax::RescueFrom::WrappedException::STATUS_MAP`

Located as of this writing, https://github.com/openstax/rescue_from/blob/implement-exception-emails/lib/openstax/rescue_from/wrapped_exception.rb#L74

{'fraction_non_alphanumeric': 0.07973856209150326, 'fraction_numerical': 0.002178649237472767, 'mean_word_length': 4.811392405063291, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1606650', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1240, 'n_words': 492}