code_error 0.9.3 → 1.0.0
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/HISTORY.md +4 -0
- data/README.md +141 -192
- data/code_error.gemspec +26 -28
- data/lib/code_error.rb +126 -67
- data/lib/code_error/version.rb +1 -1
- data/test/test_all.rb +4 -4
- data/test/test_config.rb +56 -69
- data/test/test_gen.rb +76 -0
- data/test/test_masked.rb +170 -64
- data/test/test_pos.rb +213 -0
- metadata +37 -23
- data/lib/code_error/random_token_error.rb +0 -75
- data/test/test_code_in_msg.rb +0 -85
- data/test/test_new.rb +0 -100
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 756c0d9596e3aae0b398e504ca18dd1b1fb0ccd3
|
4
|
+
data.tar.gz: 74379767b93b60733dfe447bfca14ecfd44a3b08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70918bb3ff585a75edff5cd5a6fb4d674b18ff4e143b8ac6e2806bd27d0d7737b45278821ad6709a4ea8b230564be1b728f113df7b7eb6d5b397da025bc99a9a
|
7
|
+
data.tar.gz: 88f6a362ec25aa5dcd7eed85eeb5fdf54a6f44107d8f5adafbc4bb7441d7239e31583fac19e36b4f788bbfb5ca6a83c218a4ba4afdd7c3d208c8e74f292feed1
|
data/HISTORY.md
ADDED
data/README.md
CHANGED
@@ -2,7 +2,15 @@
|
|
2
2
|
|
3
3
|
A code-based customized error.
|
4
4
|
|
5
|
-
|
5
|
+
[][gem]
|
6
|
+
[][travis]
|
7
|
+
[][coverall]
|
8
|
+
|
9
|
+
[gem]: https://rubygems.org/gems/code_error
|
10
|
+
[travis]: https://travis-ci.org/sibevin/code_error
|
11
|
+
[coverall]: https://coveralls.io/r/sibevin/code_error?branch=cover-check
|
12
|
+
|
13
|
+
raise MyError.gen(:wrong_format)
|
6
14
|
|
7
15
|
## Why?
|
8
16
|
|
@@ -16,47 +24,44 @@ It is a standard error but it provides more.
|
|
16
24
|
|
17
25
|
### Basic
|
18
26
|
|
19
|
-
Inherit CodeError::Base to create your own code-based error. You need to
|
27
|
+
Inherit CodeError::Base to create your own code-based error. You need to assign "error_codes" with a hash to define your own error code map.
|
20
28
|
|
21
29
|
class MyError < CodeError::Base
|
22
30
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
20200 => {
|
44
|
-
status: :retry,
|
45
|
-
msg: 'Client should send the IAP request again.'
|
46
|
-
}
|
31
|
+
error_codes({
|
32
|
+
purchase_info_incorrect: {
|
33
|
+
status: :failed,
|
34
|
+
msg: 'Purchase information format is incorrect.'
|
35
|
+
},
|
36
|
+
device_info_incorrect: {
|
37
|
+
status: :failed,
|
38
|
+
msg: 'Device information format is incorrect.'
|
39
|
+
},
|
40
|
+
unknown_store: {
|
41
|
+
status: :failed,
|
42
|
+
msg: 'Unknown store.'
|
43
|
+
},
|
44
|
+
duplicated_request: {
|
45
|
+
status: :duplicated,
|
46
|
+
msg: 'A duplicated IAP request is sent.'
|
47
|
+
},
|
48
|
+
resend_iap: {
|
49
|
+
status: :retry,
|
50
|
+
msg: 'Client should send the IAP request again.'
|
47
51
|
}
|
48
|
-
|
52
|
+
})
|
53
|
+
|
49
54
|
end
|
50
55
|
|
51
56
|
Raise an error with a code when you need.
|
52
57
|
|
53
|
-
raise MyError.
|
58
|
+
raise MyError.gen(:purchase_info_incorrect)
|
54
59
|
|
55
60
|
Rescue and handle it.
|
56
61
|
|
57
62
|
begin
|
58
63
|
#...
|
59
|
-
raise MyError.
|
64
|
+
raise MyError.gen(:purchase_info_incorrect)
|
60
65
|
#...
|
61
66
|
rescue MyError => e
|
62
67
|
raise e if e.internal?
|
@@ -75,93 +80,93 @@ Rescue and handle it.
|
|
75
80
|
#...
|
76
81
|
end
|
77
82
|
|
78
|
-
|
79
|
-
|
80
|
-
A customized code-based error class need to
|
81
|
-
|
82
|
-
|
83
|
-
{
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
}
|
83
|
+
#### Customize error code hash
|
84
|
+
|
85
|
+
A customized code-based error class need to assign the "error_codes" method with a hash like:
|
86
|
+
|
87
|
+
error_codes({
|
88
|
+
purchase_info_incorrect: {
|
89
|
+
status: :failed,
|
90
|
+
msg: 'Purchase information format is incorrect.'
|
91
|
+
},
|
92
|
+
device_info_incorrect: {
|
93
|
+
status: :failed,
|
94
|
+
msg: 'Device information format is incorrect.'
|
95
|
+
},
|
96
|
+
unknown_store: {
|
97
|
+
status: :failed,
|
98
|
+
msg: 'Unknown store.',
|
99
|
+
masked: true
|
100
|
+
},
|
101
|
+
duplicated_request: {
|
102
|
+
status: :duplicated,
|
103
|
+
msg: 'A duplicated IAP request is sent.',
|
104
|
+
masked: true
|
105
|
+
},
|
106
|
+
resend_iap: {
|
107
|
+
status: :retry,
|
108
|
+
msg: 'Client should send the IAP request again.'
|
105
109
|
}
|
106
|
-
|
110
|
+
})
|
107
111
|
|
108
|
-
where
|
112
|
+
where the hash key is the supported code which can be a symbol, number or string and the hash value is an another hash to store the code information corresponding each codes. The code information hash contains the following keys:
|
109
113
|
|
110
114
|
* :status - [any type you like] The error status to define the error handling flow. You can get it by calling the "status" method.
|
111
|
-
* :msg - [String] The error message. You can get it by calling the
|
112
|
-
* :masked -
|
115
|
+
* :msg - [String] The error message. You can get it by calling the `msg` method.
|
116
|
+
* :masked - [Boolean] To define the error message is masked by default or not. The default value is `false`. Please see [Mask the error message].
|
117
|
+
* :pos - [:none,:append,:prepend] Define how to show the code in the error message. The default value is `:none`. Please see [Show code in the error message].
|
113
118
|
|
114
|
-
|
119
|
+
#### Raise a code-based error
|
115
120
|
|
116
|
-
Once you
|
121
|
+
Once you setup the error codes, raising a code-based error is easy.
|
117
122
|
|
118
|
-
raise MyError.
|
123
|
+
raise MyError.gen(:purchase_info_incorrect)
|
119
124
|
|
120
|
-
There are
|
125
|
+
There are two argments in the code error constructor, `MyError.gen(code, options)`. If the given code is defined in the error_codes, the error would contain the corresponding status and msg.
|
121
126
|
|
122
|
-
e = MyError.
|
123
|
-
e.code #
|
127
|
+
e = MyError.gen(:purchase_info_incorrect)
|
128
|
+
e.code # :purchase_info_incorrect
|
124
129
|
e.status # :failed
|
125
|
-
e.msg # "Purchase information format is incorrect.
|
130
|
+
e.msg # "Purchase information format is incorrect."
|
126
131
|
|
127
|
-
You can give another string
|
132
|
+
You can give another string with the "msg" option to overwrite the original message:
|
128
133
|
|
129
|
-
e = MyError.
|
130
|
-
e.code #
|
134
|
+
e = MyError.gen(:purchase_info_incorrect, msg: "The another message which would override the original one.")
|
135
|
+
e.code # :purchase_info_incorrect
|
131
136
|
e.status # :failed
|
132
|
-
e.msg # "The another message which would override the original one.
|
137
|
+
e.msg # "The another message which would override the original one."
|
133
138
|
|
134
139
|
If the given code is unknown, the error would become an internal error, i.e., the status is the `:internal` status.
|
135
140
|
|
136
|
-
e = MyError.
|
137
|
-
e.code #
|
141
|
+
e = MyError.gen(:unknown_error)
|
142
|
+
e.code # :unknown_error
|
138
143
|
e.status # :internal
|
139
|
-
e.msg # "An internal error occurs.
|
144
|
+
e.msg # "An internal error occurs."
|
140
145
|
e.internal? # true
|
141
146
|
|
142
147
|
If a string is given, the error is also an internal error, but the "msg" would keep this string.
|
143
148
|
|
144
|
-
e = MyError.
|
145
|
-
e.code #
|
149
|
+
e = MyError.gen("Invalid input!!")
|
150
|
+
e.code # :internal_error
|
146
151
|
e.status # :internal
|
147
|
-
e.msg # "Invalid input!!"
|
152
|
+
e.msg # "Invalid input!!"
|
148
153
|
e.internal? # true
|
149
154
|
|
150
|
-
You can pass anything to the error handling
|
155
|
+
You can pass anything you want to the error handling through the "info" option.
|
151
156
|
|
152
157
|
something = 'what you want...'
|
153
|
-
e = MyError.
|
158
|
+
e = MyError.gen(:purchase_info_incorrect, info: something)
|
154
159
|
e.info # 'what you want...'
|
155
160
|
|
156
161
|
There is a special code `:success` which used in the success case.
|
157
162
|
|
158
|
-
e = MyError.
|
159
|
-
e.code #
|
163
|
+
e = MyError.gen(:success)
|
164
|
+
e.code # :success
|
160
165
|
e.status # :success
|
161
166
|
e.msg # ""
|
162
167
|
e.internal? # false
|
163
168
|
|
164
|
-
|
169
|
+
#### Handle the code-base error
|
165
170
|
|
166
171
|
When you rescue a code-based error, some useful methods are ready to use.
|
167
172
|
|
@@ -174,147 +179,91 @@ When you rescue a code-based error, some useful methods are ready to use.
|
|
174
179
|
|
175
180
|
Here is an example:
|
176
181
|
|
177
|
-
e = MyError.
|
178
|
-
e.code #
|
182
|
+
e = MyError.gen(:purchase_info_incorrect, info: 'some information')
|
183
|
+
e.code # :purchase_info_incorrect
|
179
184
|
e.status # :failed
|
180
|
-
e.msg # "Purchase information format is incorrect.
|
185
|
+
e.msg # "Purchase information format is incorrect."
|
181
186
|
e.info # "some information"
|
182
|
-
e.data # { code:
|
187
|
+
e.data # { code: :purchase_info_incorrect, status: :failed, msg: "Purchase information format is incorrect.", info: 'some information' }
|
183
188
|
e.internal? # false
|
184
189
|
|
185
190
|
### Configure your code-base error
|
186
191
|
|
187
|
-
You can
|
192
|
+
You can customize the default behavior in your code-based error. Here is an example:
|
188
193
|
|
189
194
|
class MyError < CodeError::Base
|
190
195
|
|
191
|
-
|
192
|
-
|
193
|
-
def error_codes
|
196
|
+
error_codes({
|
194
197
|
#...
|
195
|
-
|
198
|
+
})
|
199
|
+
|
200
|
+
success_config({
|
201
|
+
code: :ok,
|
202
|
+
status: :success,
|
203
|
+
msg: "My success message."
|
204
|
+
})
|
205
|
+
|
206
|
+
internal_config({
|
207
|
+
code: :oops,
|
208
|
+
status: :failed,
|
209
|
+
msg: "My internal error message."
|
210
|
+
})
|
211
|
+
|
212
|
+
masked_msg "My masked message."
|
196
213
|
|
197
|
-
def config
|
198
|
-
super.merge({
|
199
|
-
success: {
|
200
|
-
code: CodeError::SUCCESS_CODE,
|
201
|
-
status: CodeError::SUCCESS_STATUS,
|
202
|
-
msg: "My own success message."
|
203
|
-
},
|
204
|
-
internal: {
|
205
|
-
code: CodeError::INTERNAL_CODE,
|
206
|
-
status: CodeError::INTERNAL_STATUS,
|
207
|
-
msg: "My own internal error message."
|
208
|
-
},
|
209
|
-
masked_msg: "My own masked message",
|
210
|
-
append_code_in_msg: :none
|
211
|
-
})
|
212
|
-
end
|
213
214
|
end
|
214
215
|
|
215
|
-
where the
|
216
|
+
where the configures are:
|
216
217
|
|
217
|
-
*
|
218
|
-
*
|
219
|
-
*
|
220
|
-
*
|
218
|
+
* success_config - [Hash] Define the success code, status and message. The default value is `{ code: CodeError::SUCCESS_CODE, status: CodeError::SUCCESS_STATUS, msg: "" }`.
|
219
|
+
* internal_config - [Hash] Define the internal error code, status and message. The default value is `{ code: CodeError::INTERNAL_CODE, status: CodeError::INTERNAL_STATUS, msg: "" }`.
|
220
|
+
* masked_msg - [String] Define the message to replace the masked one. The default value is `CodeError::MASKED_MSG`.
|
221
|
+
* masked - [Boolean] To define the error message is masked by default or not. The default value is `false`. Please see [Mask the error message].
|
222
|
+
* pos - [:none,:append,:prepend] Define how to show the code in the error message. The default value is `:none`. Please see [Show code in the error message].
|
221
223
|
|
222
|
-
|
224
|
+
#### Mask the error message
|
223
225
|
|
224
226
|
You can mask an error message if you don't want to show it. A masked message would be replaced with the default masked message. There are several ways to do it, they are listed below sorted by the precedence.
|
225
227
|
|
226
|
-
* Pass `true` to the
|
228
|
+
* Pass `masked: true` to the `msg` or `data` method.
|
227
229
|
|
228
|
-
e = MyError.
|
229
|
-
e.msg(ture) # "An error occurs.
|
230
|
-
e.data(true) # { code:
|
230
|
+
e = MyError.gen(:purchase_info_incorrect)
|
231
|
+
e.msg(masked: ture) # "An error occurs."
|
232
|
+
e.data(masked: true) # { code: :purchase_info_incorrect, status: :failed, msg: "An error occurs.", info: {} }
|
231
233
|
|
232
|
-
* Pass the
|
234
|
+
* Pass the `masked: true` to the `gen` method.
|
233
235
|
|
234
|
-
e = MyError.
|
235
|
-
e.msg # "An error occurs.
|
236
|
+
e = MyError.gen(:purchase_info_incorrect, masked: true)
|
237
|
+
e.msg # "An error occurs."
|
236
238
|
|
237
239
|
* Add `masked: true` in the error_codes hash for particular error. Please see [Customize error code hash].
|
238
240
|
|
239
|
-
|
241
|
+
* Call `masked true` in your error class. Please see [Configure your code-base error].
|
240
242
|
|
241
|
-
|
243
|
+
The default masked message can be customized to your own message by calling the `masked_msg` in your error class. Please see [Configure your code-base error].
|
242
244
|
|
243
|
-
|
245
|
+
#### Show code in the error message
|
244
246
|
|
245
|
-
|
247
|
+
You can show code in the error message by giving a `pos` option. The supported options are `:none`, `:append`, and `:prepend`.
|
246
248
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
{
|
251
|
-
20001 => {
|
252
|
-
status: :failed,
|
253
|
-
msg: 'code_error.my_error.purchase_info_incorrect'
|
254
|
-
},
|
255
|
-
20002 => {
|
256
|
-
status: :failed,
|
257
|
-
msg: 'code_error.my_error.device_info_incorrect'
|
258
|
-
},
|
259
|
-
20003 => {
|
260
|
-
status: :failed,
|
261
|
-
msg: 'code_error.my_error.unknown_store'
|
262
|
-
},
|
263
|
-
20100 => {
|
264
|
-
status: :duplicated,
|
265
|
-
msg: 'code_error.my_error.duplicated_request'
|
266
|
-
},
|
267
|
-
20200 => {
|
268
|
-
status: :retry,
|
269
|
-
msg: 'code_error.my_error.retry'
|
270
|
-
}
|
271
|
-
}
|
272
|
-
end
|
249
|
+
* :none - (Default) Don't show code in the error message.
|
250
|
+
* :append - Append the code after the error message. For example: "Purchase information format is incorrect. (purchase_info_incorrect)"
|
251
|
+
* :prepend - Prepend the code before the error message. For example: "(purchase_info_incorrect) Purchase information format is incorrect."
|
273
252
|
|
274
|
-
|
275
|
-
super.merge({
|
276
|
-
success: {
|
277
|
-
code: CodeError::SUCCESS_CODE,
|
278
|
-
status: CodeError::SUCCESS_STATUS,
|
279
|
-
msg: 'code_error_my_error.success_msg'
|
280
|
-
},
|
281
|
-
internal: {
|
282
|
-
code: CodeError::INTERNAL_CODE,
|
283
|
-
status: CodeError::INTERNAL_STATUS,
|
284
|
-
msg: 'code_error_my_error.internal_msg'
|
285
|
-
},
|
286
|
-
masked_msg: 'code_error_my_error.masked_msg',
|
287
|
-
append_code_in_msg: :append
|
288
|
-
})
|
289
|
-
end
|
290
|
-
end
|
253
|
+
Same as the "mask" feature, there are several ways to do it, they are listed below sorted by the precedence.
|
291
254
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
elsif code_in_message == :prepand
|
300
|
-
m = I18n.t('code_error.my_error.prepend_code_msg', msg: m, code: code)
|
301
|
-
end
|
302
|
-
end
|
303
|
-
m
|
304
|
-
end
|
255
|
+
* Pass `pos` option to the `msg` or `data` method.
|
256
|
+
|
257
|
+
e = MyError.gen(:purchase_info_incorrect)
|
258
|
+
e.msg(pos: :append) # "Purchase information format is incorrect. (purchase_info_incorrect)"
|
259
|
+
e.data(pos: :prepend, masked: true) # { code: :purchase_info_incorrect, status: :failed, msg: "(purchase_info_incorrect) An error occurs.", info: {} }
|
260
|
+
|
261
|
+
* Pass the `pos` option to the `gen` method.
|
305
262
|
|
306
|
-
|
263
|
+
e = MyError.gen(:purchase_info_incorrect, pos: :append)
|
264
|
+
e.msg # "Purchase information format is incorrect. (purchase_info_incorrect)"
|
307
265
|
|
308
|
-
|
309
|
-
code_error:
|
310
|
-
my_error:
|
311
|
-
append_code_msg: "%{msg}(%{code})"
|
312
|
-
prepend_code_msg: "(%{code})%{msg}"
|
313
|
-
purchase_info_invalid: "Purchase information format is incorrect."
|
314
|
-
device_info_invalid: "Device information format is incorrect."
|
315
|
-
unknown_store: "Unknown Store."
|
316
|
-
duplicated_request: "A duplicated IAP request is sent."
|
317
|
-
retry: "Client should send the IAP request again."
|
266
|
+
* Call `pos` in your error class. Please see [Configure your code-base error].
|
318
267
|
|
319
268
|
## Test
|
320
269
|
|
@@ -328,4 +277,4 @@ Sibevin Wang
|
|
328
277
|
|
329
278
|
## Copyright
|
330
279
|
|
331
|
-
Copyright (c) 2013 Sibevin Wang. Released under the MIT license.
|
280
|
+
Copyright (c) 2013 - 2014 Sibevin Wang. Released under the MIT license.
|