json_api_errors 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json_api_errors/default/code.rb +21 -0
- data/lib/json_api_errors/default/detail.rb +21 -0
- data/lib/json_api_errors/default/error.rb +18 -0
- data/lib/json_api_errors/default/id.rb +21 -0
- data/lib/json_api_errors/default/links.rb +26 -0
- data/lib/json_api_errors/default/meta.rb +23 -0
- data/lib/json_api_errors/default/source.rb +31 -0
- data/lib/json_api_errors/default/status.rb +21 -0
- data/lib/json_api_errors/default/title.rb +23 -0
- data/lib/json_api_errors/error.rb +134 -0
- data/lib/json_api_errors/error_collection.rb +23 -0
- data/lib/json_api_errors/version.rb +1 -1
- data/lib/json_api_errors.rb +2 -0
- metadata +12 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36ac2c5fb9d968b75836ce8791362284e788566b
|
4
|
+
data.tar.gz: 28d1da3d6d77f5a8c8a4d27ce1bfe6f4437e361c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67fe62cc645b89a55c5743b8717cc7854cc59a7ba034aed0e112a9fd025c2bbfd8cdf7aae5aa329485b636f37a2e5edc6c610e62eb892b5fa102eac9e9ac826a
|
7
|
+
data.tar.gz: 671bfedcf9fdc3a951b42497d707a0aed0873356980dcc2939678e0391d54dea56df228bb510f900cb262856208c35a74eb4adb52fbddb964416ab0ec9248ed0
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# DefaultCode
|
2
|
+
# ===========
|
3
|
+
#
|
4
|
+
# This is the default error code. To override this you can create your own
|
5
|
+
# class that implements #to_s and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(status: MySpecialErrorCodeClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# an application-specific error code, expressed as a string value.
|
13
|
+
module JsonApiErrors
|
14
|
+
module Default
|
15
|
+
class Code
|
16
|
+
def to_s
|
17
|
+
"default-code"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# DefaultDetail
|
2
|
+
# =============
|
3
|
+
#
|
4
|
+
# This is the default error detail. To override this you can create your own
|
5
|
+
# class that implements #to_s and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(detail: MySpecialErrorDetailClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# a human-readable explanation specific to this occurrence of the problem.
|
13
|
+
module JsonApiErrors
|
14
|
+
module Default
|
15
|
+
class Detail
|
16
|
+
def to_s
|
17
|
+
"default-detail"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module JsonApiErrors
|
2
|
+
module Default
|
3
|
+
class Error
|
4
|
+
def call(error)
|
5
|
+
{
|
6
|
+
id: error.id.to_s,
|
7
|
+
links: error.links.to_h,
|
8
|
+
status: error.status.to_s,
|
9
|
+
code: error.code.to_s,
|
10
|
+
title: error.title.to_s,
|
11
|
+
detail: error.detail.to_s,
|
12
|
+
source: error.source.to_h,
|
13
|
+
meta: error.meta.to_h
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# DefaultId
|
2
|
+
# =========
|
3
|
+
#
|
4
|
+
# This is the default error id. To override this you can create your own
|
5
|
+
# class that implements #to_s and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(id: MySpecialErrorIdClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# a unique identifier for this particular occurrence of the problem.
|
13
|
+
module JsonApiErrors
|
14
|
+
module Default
|
15
|
+
class Id
|
16
|
+
def to_s
|
17
|
+
"default-id"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# DefaultLinks
|
2
|
+
# ============
|
3
|
+
#
|
4
|
+
# This is the default error links. To override this you can create your own
|
5
|
+
# class that implements #to_hash and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(links: MySpecialErrorLinksClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# a links object containing the following members:
|
13
|
+
#
|
14
|
+
# about: a link that leads to further details about this particular
|
15
|
+
# occurrence of the problem.
|
16
|
+
module JsonApiErrors
|
17
|
+
module Default
|
18
|
+
class Links
|
19
|
+
def to_h
|
20
|
+
{
|
21
|
+
about: "default-links"
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# DefaultMeta
|
2
|
+
# ===========
|
3
|
+
#
|
4
|
+
# This is the default error meta information. To override this you can create your own
|
5
|
+
# class that implements #to_hash and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(meta: MySpecialErrorMetaClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# a meta object containing non-standard meta-information about the error.
|
13
|
+
module JsonApiErrors
|
14
|
+
module Default
|
15
|
+
class Meta
|
16
|
+
def to_h
|
17
|
+
{
|
18
|
+
extra_info: "default-meta"
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# DefaultSource
|
2
|
+
# =============
|
3
|
+
#
|
4
|
+
# This is the default error source. To override this you can create your own
|
5
|
+
# class that implements #to_hash and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(source: MySpecialErrorSourceClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# an object containing references to the source of the error, optionally
|
13
|
+
# including any of the following members:
|
14
|
+
#
|
15
|
+
# pointer: a JSON Pointer [RFC6901] to the associated entity in the
|
16
|
+
# request document [e.g. "/data" for a primary data object,
|
17
|
+
# or "/data/attributes/title" for a specific attribute].
|
18
|
+
#
|
19
|
+
# parameter: a string indicating which query parameter caused the error.
|
20
|
+
module JsonApiErrors
|
21
|
+
module Default
|
22
|
+
class Source
|
23
|
+
def to_h
|
24
|
+
{
|
25
|
+
pointer: "default-pointer",
|
26
|
+
parameter: "default-parameter"
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# DefaultStatus
|
2
|
+
# =============
|
3
|
+
#
|
4
|
+
# This is the default error status code. To override this you can create your own
|
5
|
+
# class that implements #to_s and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(status: MySpecialErrorStatusClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# the HTTP status code applicable to this problem, expressed as a string value.
|
13
|
+
module JsonApiErrors
|
14
|
+
module Default
|
15
|
+
class Status
|
16
|
+
def to_s
|
17
|
+
"422"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# DefaultTitle
|
2
|
+
# ============
|
3
|
+
#
|
4
|
+
# This is the default error title. To override this you can create your own
|
5
|
+
# class that implements #to_s and inject it into the constructor
|
6
|
+
# like this:
|
7
|
+
#
|
8
|
+
# JsonAPI::Error.new(title: MySpecialErrorTitleClass.new)
|
9
|
+
#
|
10
|
+
# From the api docs:
|
11
|
+
#
|
12
|
+
# a short, human-readable summary of the problem that SHOULD NOT change
|
13
|
+
# from occurrence to occurrence of the problem, except for purposes of
|
14
|
+
# localization.
|
15
|
+
module JsonApiErrors
|
16
|
+
module Default
|
17
|
+
class Title
|
18
|
+
def to_s
|
19
|
+
"default-title"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require "json_api_errors/default/id"
|
2
|
+
require "json_api_errors/default/code"
|
3
|
+
require "json_api_errors/default/status"
|
4
|
+
require "json_api_errors/default/title"
|
5
|
+
require "json_api_errors/default/detail"
|
6
|
+
require "json_api_errors/default/links"
|
7
|
+
require "json_api_errors/default/source"
|
8
|
+
require "json_api_errors/default/meta"
|
9
|
+
require "json_api_errors/default/error"
|
10
|
+
|
11
|
+
# JsonApiErrors::Error
|
12
|
+
# ====================
|
13
|
+
#
|
14
|
+
# An error object that by default conforms to the JsonAPI error spec.
|
15
|
+
# See: http://jsonapi.org/format/#errors
|
16
|
+
#
|
17
|
+
# When you initialize a new instance of JsonApiErrors::Error without any
|
18
|
+
# arguments, you receive an error object with default values. The defaults
|
19
|
+
# only serve to demonstrate what an error following the entire spec looks like.
|
20
|
+
# Other than that they are not helpful because it's difficult to know exactly
|
21
|
+
# what a default error means. So you need to configure the error you wish to generate.
|
22
|
+
#
|
23
|
+
# There are two parts to configuring an error object:
|
24
|
+
#
|
25
|
+
# First, you'll need to create a template.
|
26
|
+
#
|
27
|
+
# Second, you'll need to configure the attributes you want to be displayed
|
28
|
+
# in the template.
|
29
|
+
#
|
30
|
+
# The easiest way to do this is by creating a lambda whose body is a hash
|
31
|
+
# representing the template you wish to be displayed. Then pass a block to the
|
32
|
+
# initializer:
|
33
|
+
#
|
34
|
+
# template = ->(error) do
|
35
|
+
# {
|
36
|
+
# status: error.status,
|
37
|
+
# code: error.code
|
38
|
+
# }
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# error = JsonApiErrors::Error.new do |config|
|
42
|
+
# config.error = template
|
43
|
+
# config.status = "400"
|
44
|
+
# config.code = "Bad Request"
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# error.call => { status: "400", code: "Bad Request" }
|
48
|
+
#
|
49
|
+
#
|
50
|
+
# You can also pass in literal values for the attributes for the attributes
|
51
|
+
# as well as inject them into the initializer:
|
52
|
+
#
|
53
|
+
# JsonApiErrors::Error.new( error: CustomError.new,
|
54
|
+
# status: CustomStatus.new,
|
55
|
+
# code: CustomCode.new,
|
56
|
+
# links: CustomLinks.new )
|
57
|
+
#
|
58
|
+
#
|
59
|
+
# error.call => { status: "400",
|
60
|
+
# code: "Bad Request",
|
61
|
+
# links: {
|
62
|
+
# about: "www.example.org"
|
63
|
+
# }
|
64
|
+
# }
|
65
|
+
#
|
66
|
+
# Here's how you would define those classes:
|
67
|
+
#
|
68
|
+
# class CustomError
|
69
|
+
# def call(error)
|
70
|
+
# {
|
71
|
+
# status: error.status.to_s,
|
72
|
+
# code: error.code.to_s,
|
73
|
+
# links: error.links.to_h
|
74
|
+
# }
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# class CustomStatus
|
79
|
+
# def to_s; "400"; end
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# class CustomCode
|
83
|
+
# def to_s; "Bad Request"; end
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# class CustomLinks
|
87
|
+
# def to_h; { about: "www.example.com" }; end
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# Note:
|
91
|
+
# =====
|
92
|
+
# The error class must implement #call and it receives an error object as an argument.
|
93
|
+
# Classes that define id, status, code, title, detail must implement #to_s.
|
94
|
+
# Classes that define links, source, and meta must implement #to_h.
|
95
|
+
#
|
96
|
+
|
97
|
+
module JsonApiErrors
|
98
|
+
class Error
|
99
|
+
|
100
|
+
attr_accessor :error, :id, :status, :links, :code, :title, :detail,
|
101
|
+
:source, :meta
|
102
|
+
|
103
|
+
def initialize( error: JsonApiErrors::Default::Error.new,
|
104
|
+
id: JsonApiErrors::Default::Id.new,
|
105
|
+
status: JsonApiErrors::Default::Status.new,
|
106
|
+
code: JsonApiErrors::Default::Code.new,
|
107
|
+
links: JsonApiErrors::Default::Links.new,
|
108
|
+
title: JsonApiErrors::Default::Title.new,
|
109
|
+
detail: JsonApiErrors::Default::Detail.new,
|
110
|
+
source: JsonApiErrors::Default::Source.new,
|
111
|
+
meta: JsonApiErrors::Default::Meta.new )
|
112
|
+
|
113
|
+
@error = error
|
114
|
+
@id = id
|
115
|
+
@status = status
|
116
|
+
@code = code
|
117
|
+
@links = links
|
118
|
+
@title = title
|
119
|
+
@detail = detail
|
120
|
+
@source = source
|
121
|
+
@meta = meta
|
122
|
+
|
123
|
+
yield(self) if block_given?
|
124
|
+
end
|
125
|
+
|
126
|
+
def call
|
127
|
+
error.call(self)
|
128
|
+
end
|
129
|
+
|
130
|
+
def status_code
|
131
|
+
status.to_s
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module JsonApiErrors
|
4
|
+
class ErrorCollection < DelegateClass(Array)
|
5
|
+
def initialize
|
6
|
+
super([])
|
7
|
+
end
|
8
|
+
|
9
|
+
def add_error(error)
|
10
|
+
self << error
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
{
|
15
|
+
errors: self.map! { |e| e.call }
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def generic_status
|
20
|
+
200
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/json_api_errors.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_errors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- byelipk
|
@@ -72,6 +72,17 @@ files:
|
|
72
72
|
- bin/setup
|
73
73
|
- json_api_errors.gemspec
|
74
74
|
- lib/json_api_errors.rb
|
75
|
+
- lib/json_api_errors/default/code.rb
|
76
|
+
- lib/json_api_errors/default/detail.rb
|
77
|
+
- lib/json_api_errors/default/error.rb
|
78
|
+
- lib/json_api_errors/default/id.rb
|
79
|
+
- lib/json_api_errors/default/links.rb
|
80
|
+
- lib/json_api_errors/default/meta.rb
|
81
|
+
- lib/json_api_errors/default/source.rb
|
82
|
+
- lib/json_api_errors/default/status.rb
|
83
|
+
- lib/json_api_errors/default/title.rb
|
84
|
+
- lib/json_api_errors/error.rb
|
85
|
+
- lib/json_api_errors/error_collection.rb
|
75
86
|
- lib/json_api_errors/version.rb
|
76
87
|
homepage: https://github.com/byelipk/json_api_errors
|
77
88
|
licenses:
|