Skip to content

Commit 2cbc3d8

Browse files
committed
Merge pull request #164 from dennisfaust/text-error-codes
Text error codes
2 parents 794bc89 + efaa23d commit 2cbc3d8

4 files changed

Lines changed: 51 additions & 3 deletions

File tree

lib/jsonapi/configuration.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class Configuration
99
:allowed_request_params,
1010
:default_paginator,
1111
:default_page_size,
12-
:maximum_page_size
12+
:maximum_page_size,
13+
:use_text_errors
1314

1415
def initialize
1516
#:underscored_key, :camelized_key, :dasherized_key, or custom
@@ -25,6 +26,7 @@ def initialize
2526

2627
self.default_page_size = 10
2728
self.maximum_page_size = 20
29+
self.use_text_errors = false
2830
end
2931

3032
def json_key_format=(format)
@@ -52,6 +54,10 @@ def default_page_size=(default_page_size)
5254
def maximum_page_size=(maximum_page_size)
5355
@maximum_page_size = maximum_page_size
5456
end
57+
58+
def use_text_errors=(use_text_errors)
59+
@use_text_errors = use_text_errors
60+
end
5561
end
5662

5763
class << self

lib/jsonapi/error.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ def initialize(options={})
88
@detail = options[:detail]
99
@id = options[:id]
1010
@href = options[:href]
11-
@code = options[:code]
11+
@code = if JSONAPI.configuration.use_text_errors
12+
TEXT_ERRORS[options[:code]]
13+
else
14+
options[:code]
15+
end
1216
@path = options[:path]
1317
@links = options[:links]
1418
@status = options[:status]
1519
end
1620
end
17-
end
21+
end

lib/jsonapi/error_codes.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,30 @@ module JSONAPI
2323
RECORD_NOT_FOUND = 404
2424
UNSUPPORTED_MEDIA_TYPE = 415
2525
LOCKED = 423
26+
27+
TEXT_ERRORS =
28+
{ VALIDATION_ERROR => 'VALIDATION_ERROR',
29+
INVALID_RESOURCE => 'INVALID_RESOURCE',
30+
FILTER_NOT_ALLOWED => 'FILTER_NOT_ALLOWED',
31+
INVALID_FIELD_VALUE => 'INVALID_FIELD_VALUE',
32+
INVALID_FIELD => 'INVALID_FIELD',
33+
PARAM_NOT_ALLOWED => 'PARAM_NOT_ALLOWED',
34+
PARAM_MISSING => 'PARAM_MISSING',
35+
INVALID_FILTER_VALUE => 'INVALID_FILTER_VALUE',
36+
COUNT_MISMATCH => 'COUNT_MISMATCH',
37+
KEY_ORDER_MISMATCH => 'KEY_ORDER_MISMATCH',
38+
KEY_NOT_INCLUDED_IN_URL => 'KEY_NOT_INCLUDED_IN_URL',
39+
INVALID_INCLUDE => 'INVALID_INCLUDE',
40+
RELATION_EXISTS => 'RELATION_EXISTS',
41+
INVALID_SORT_CRITERIA => 'INVALID_SORT_CRITERIA',
42+
INVALID_LINKS_OBJECT => 'INVALID_LINKS_OBJECT',
43+
TYPE_MISMATCH => 'TYPE_MISMATCH',
44+
INVALID_PAGE_OBJECT => 'INVALID_PAGE_OBJECT',
45+
INVALID_PAGE_VALUE => 'INVALID_PAGE_VALUE',
46+
INVALID_SORT_FORMAT => 'INVALID_SORT_FORMAT',
47+
INVALID_FIELD_FORMAT => 'INVALID_FIELD_FORMAT',
48+
FORBIDDEN => 'FORBIDDEN',
49+
RECORD_NOT_FOUND => 'RECORD_NOT_FOUND',
50+
UNSUPPORTED_MEDIA_TYPE => 'UNSUPPORTED_MEDIA_TYPE',
51+
LOCKED => 'LOCKED' }
2652
end

test/controllers/controller_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ def set_content_type_header!
44
@request.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
55
end
66

7+
class ConfigControllerTest < ActionController::TestCase
8+
9+
end
10+
711
class PostsControllerTest < ActionController::TestCase
812
def test_index
913
get :index
@@ -1388,6 +1392,14 @@ def setup
13881392
JSONAPI.configuration.json_key_format = :camelized_key
13891393
end
13901394

1395+
def test_text_error
1396+
JSONAPI.configuration.use_text_errors = true
1397+
get :index, {sort: 'not_in_record'}
1398+
assert_response 400
1399+
assert_equal 'INVALID_SORT_FORMAT', json_response['errors'][0]['code']
1400+
JSONAPI.configuration.use_text_errors = false
1401+
end
1402+
13911403
def test_expense_entries_index
13921404
get :index
13931405
assert_response :success

0 commit comments

Comments
 (0)