Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
changes as per PR
Browse files Browse the repository at this point in the history
  • Loading branch information
sheavalentine-mdsol committed Aug 6, 2014
1 parent fe1abbc commit cdd3bf9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.bundle
.idea*
api_descriptors/
gemfiles/*.lock
Gemfile.lock
Expand Down
38 changes: 20 additions & 18 deletions doc/handling_errors.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
# @title Handling Errors

## Simple Setup
Crichton provides default erroring functionality through a rails generator. In this case you can simply run
"rails generate errors_description [path to errors class]" it also optionally takes a resource name as a second
Crichton provides default error functionality through a rails generator. In this case you can simply run
"bundle exec rails generate crichton:errors_description ./lib/". It also optionally takes a resource name as a second
argument if you don't like the default of "Errors", and an api directory as a third argument if you have your
descriptors somewhere other than 'api_descriptors'.

## Using Errors
To use errors, simply instantiate your errors class with the attributes you want and render them in your controller.

```ruby
def index
if (params[:search_term] == 'search')
error = Error.new({ title: 'Not supported search term',
error_code: :search_term_is_not_supported,
http_status: 422,
details: 'You requested search but it is not a valid search_term',
controller: self})
respond_with(error, status: 404)
else
@drds = Drds.find(params[:search_term])
respond_with(@drds, options)
end
end
require 'crichton_error'

errors = Crichton_error.new({
title: e.message,
details: '',
error_code: error_cause,
http_status: 422,
stack_trace: '',
controller: self
})

respond_to do |format|
format.html { render text: errors.to_media_type(:xhtml, { semantics: :styled_microdata }), status: error_cause }
format.hale_json { render text: errors.to_media_type(:hale_json), status: error_cause }
end
```

## Customizing
To customize Error handling, you can simply extend the functionality generated, or add create your own class and
descriptor file to generate the Errors resource. For example if we wanted to add a remedy link we may extend the
To customize Error handling, you can simply extend the functionality generated, or create your own class and
descriptor file to generate the Errors resource. For example, if we wanted to add a remedy link we may extend the
default functionality like so:

### Custom Errors Descriptor
Expand Down Expand Up @@ -141,4 +143,4 @@ To customize Error handling, you can simply extend the functionality generated,
end
```



10 changes: 2 additions & 8 deletions spec/lib/crichton/tools/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,14 @@ def remedy_url
end
let(:serializer) { HaleJsonSerializer }

it 'self-registers as a serializer for the hale+json media-type' do
expect(Serializer.registered_serializers[:hale_json]).to eq(serializer)
end

describe '#as_media_type' do
after do
it 'returns an error body' do
@hale = JSON.load(drds_hale_json)
stub_example_configuration
Crichton.initialize_registry(@document || errors_descriptor)
expect(serializer.new(drds).as_media_type(conditions: 'can_do_anything')['details']).to eq("You requested search but it is not a valid search_term")
end

it 'returns an error body' do
@hale = JSON.load(drds_hale_json)
end
end
end
end
Expand Down

0 comments on commit cdd3bf9

Please sign in to comment.