Skip to content

Commit dd51ec3

Browse files
author
Sebastian Wilgosz
authored
Merge pull request #18 from driggl/12-improve-documentation
Improve documentation
2 parents 3fcb64d + d69c2a6 commit dd51ec3

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

README.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
3232
```
3333

3434
From this point you'll have default html errors being serialized. JsonapiErrorsHandler offers 4 predefined errors:
35-
- JsonapiErrorsHandler::Errors::Invalid
36-
- JsonapiErrorsHandler::Errors::Forbidden
37-
- JsonapiErrorsHandler::Errors::NotFound
38-
- JsonapiErrorsHandler::Errors::Unauthorized
35+
- [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
36+
- [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
37+
- [JsonapiErrorsHandler::Errors::NotFound](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
38+
- [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
3939

4040
If you rise any of errors above in any place of your application, client gets the nicely formatted error message instead of 500
4141

@@ -52,6 +52,57 @@ ErrorsMapper.map_errors!({
5252
rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
5353
```
5454

55+
###Custom error logging
56+
57+
When you'll include the `jsonapi_errors_handler` to your controller, all errors will be handled and delivered to the client in the nice, formatted
58+
way.
59+
60+
However, you'd probably like to have a way to log the risen error on your own to send notifications to developers that
61+
something unexpected happened.
62+
63+
To do so, just implement the `log_error` method in your controller, that accepts the risen error as an argument.
64+
65+
```
66+
def log_error(error)
67+
#do the fancy logging here
68+
end
69+
```
70+
71+
### Custom error responses.
72+
73+
By default, we deliver hardcoded responses. You can check out the defined error classes for details
74+
75+
- [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
76+
- [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
77+
- [JsonapiErrorsHandler::Errors::NotFound](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
78+
- [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
79+
80+
If you want to have custom error responses being delivered, just create your own `Api::Errors` that inherits from `JsonapiErrorsHandler::StandardError`
81+
82+
### Localization example
83+
84+
If you want to localize your responses, just create a class:
85+
86+
```
87+
module Api::Errors
88+
class Forbidden < ::JsonapiErrorsHandler::Errors::StandardError
89+
def initialize(*)
90+
super(
91+
title: I18n.t('api.errors.forbidden.title'),
92+
status: 403,
93+
detail: I18n.t('api.errors.forbidden.detail'),
94+
source: { pointer: '/request/headers/authorization' }
95+
)
96+
end
97+
end
98+
end
99+
```
100+
101+
## Guides & tutorials
102+
103+
- [Handling Exceptions in Rails Applications](https://driggl.com/blog/a/handling-exceptions-in-rails-applications) - Gem's concept explained in details
104+
- [JsonApi Errors Handler Guide](https://driggl.com/blog/a/json-api-errors-handler)
105+
55106
## Development
56107

57108
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -65,6 +116,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/driggl
65116
**How to contribute:**
66117

67118
1. Fork repository
119+
2. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
68120
2. Commit changes
69121
- Keep commits small and atomic
70122
- Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)

0 commit comments

Comments
 (0)