Skip to content

Commit 49465d7

Browse files
Updated Readme
1 parent 453e1b5 commit 49465d7

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

README.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,16 @@ RECORD_NOT_DESTROYED | :forbidden (403)
242242
FORBIDDEN_RESOURCE | :forbidden (403)
243243
UNAUTHORIZED_ACCESS | :unauthorized (401)
244244

245-
For futher information, take a look at the [Code](https://github.com/kalidasm/rails-api-response-builder/blob/master/lib/api/exception.rb)
245+
For detailed information, feel free to dive into the [Code](https://github.com/kalidasm/rails-api-response-builder/blob/master/lib/api/exception.rb)
246246

247247

248248
##### Error Messages for API Exceptions
249249

250-
All the error messages for these exceptions are rendered from I18n locales (`en.api_response.messages.#{key}`)
250+
All the error messages are internalized using `Rails Internationalization (I18n) API`.
251251

252-
Key represents lower-cased version of exception names listed above
252+
Exceptional messages are mapped under I18n locales (`en.api_response.messages.#{key}`).
253+
254+
Key represents lower-cased version of exception names listed above.
253255

254256
Example :
255257

@@ -265,17 +267,26 @@ Example :
265267
unauthorized_access: "You are not authorized to perform this operation"
266268
```
267269
268-
##### Raising API Exception inside controllers
270+
##### Raising API Exception inside controller actions
269271
270272
```ruby
271-
def update_credit_score
272-
# code to authorization info for current user
273-
unless user_authorized?
273+
class CreditsController < ApplicationController
274+
before_action :authorize_user, only: :update
275+
276+
def update
277+
@credit.update(credit_params)
278+
end
279+
280+
private
281+
282+
def authorize_user
283+
return if user_authorized?
284+
285+
# Will be handled by rescue_from methods in application controller
274286
raise ::Api::Exception.new(
275287
::Api::Exception.unauthorized_access
276288
)
277289
end
278-
# Code to Update the credit score
279290
end
280291
```
281292

@@ -295,23 +306,21 @@ Response would be
295306
If you want to override the default error message in a specific scenario,
296307

297308
```ruby
298-
def update_credit_score
299-
# code to authorization info for current user
300-
unless user_authorized?
301-
custom_error_message = 'You are not allowed to update credit score'
309+
private
302310

303-
raise ::Api::Exception.new(
304-
::Api::Exception.unauthorized_access, custom_error_message
305-
)
306-
end
307-
# Code to Update the credit score
311+
def authorize_user
312+
return if user_authorized?
313+
314+
raise ::Api::Exception.new(
315+
::Api::Exception.unauthorized_access, "Looks like you are not authorized to update credit score"
316+
)
308317
end
309318
```
310319
```json
311320
{
312321
"status": "failure",
313322
"messages": {
314-
"errors": ["You are not allowed to update credit score"]
323+
"errors": ["Looks like you are not authorized to update credit score"]
315324
},
316325
"status_code": "unprocessable_entity",
317326
"meta": {}
@@ -320,7 +329,7 @@ If you want to override the default error message in a specific scenario,
320329

321330
#### Response Status - Failure / Success
322331

323-
Response Object contains a key named `status`. It will have only any one of
332+
Response Object contains a key named `status`. It will have only any one of following types
324333
* failure
325334
* success
326335

0 commit comments

Comments
 (0)