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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d3630c871f307f8a79b4c038efac86c2e7f7031
4
- data.tar.gz: 9c7b3e960adb94896f0d062e5eebffc45bf77ccd
3
+ metadata.gz: 756c0d9596e3aae0b398e504ca18dd1b1fb0ccd3
4
+ data.tar.gz: 74379767b93b60733dfe447bfca14ecfd44a3b08
5
5
  SHA512:
6
- metadata.gz: 0ffd780bcf90b43be9c7fa2e6c52ff973623a154ce99edeb1962c25805da98aad659e7969a5461087cd5dc5b84fd3ddae09130308c9b5b9c747a7e26931cc073
7
- data.tar.gz: 225ab9e1a644db1de28e03d6b3f4518e10a39905f5868d2ab890c174240c18a342f422b8a919b57c754200a67f0f9c44d724212019aac18b4f4942b44cb782e7
6
+ metadata.gz: 70918bb3ff585a75edff5cd5a6fb4d674b18ff4e143b8ac6e2806bd27d0d7737b45278821ad6709a4ea8b230564be1b728f113df7b7eb6d5b397da025bc99a9a
7
+ data.tar.gz: 88f6a362ec25aa5dcd7eed85eeb5fdf54a6f44107d8f5adafbc4bb7441d7239e31583fac19e36b4f788bbfb5ca6a83c218a4ba4afdd7c3d208c8e74f292feed1
data/HISTORY.md ADDED
@@ -0,0 +1,4 @@
1
+ ## v1.0.0 / 2014-12-02
2
+
3
+ * [Init] Initial Release
4
+
data/README.md CHANGED
@@ -2,7 +2,15 @@
2
2
 
3
3
  A code-based customized error.
4
4
 
5
- raise MyError.new(10002)
5
+ [![Gem Version](https://badge.fury.io/rb/code_error.png)][gem]
6
+ [![Build Status](https://travis-ci.org/sibevin/code_error.svg?branch=build)][travis]
7
+ [![Coverage Status](https://coveralls.io/repos/sibevin/code_error/badge.png?branch=cover-check)][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 implement the "error_codes" method which provides a hash to define your own error code map.
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
- private
24
-
25
- def error_codes
26
- {
27
- 20001 => {
28
- status: :failed,
29
- msg: 'Purchase information format is incorrect.'
30
- },
31
- 20002 => {
32
- status: :failed,
33
- msg: 'Device information format is incorrect.'
34
- },
35
- 20003 => {
36
- status: :failed,
37
- msg: 'Unknown store.'
38
- },
39
- 20100 => {
40
- status: :duplicated,
41
- msg: 'A duplicated IAP request is sent.'
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
- end
52
+ })
53
+
49
54
  end
50
55
 
51
56
  Raise an error with a code when you need.
52
57
 
53
- raise MyError.new(20001)
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.new(20001)
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
- ### Customize error code hash
79
-
80
- A customized code-based error class need to implement the "error_codes" method which provides a hash like:
81
-
82
- def error_codes
83
- {
84
- 20001 => {
85
- status: :failed,
86
- msg: 'Purchase information format is incorrect.'
87
- },
88
- 20002 => {
89
- status: :failed,
90
- msg: 'Device information format is incorrect.'
91
- },
92
- 20003 => {
93
- status: :failed,
94
- msg: 'Unknown store.'
95
- },
96
- 20100 => {
97
- status: :duplicated,
98
- msg: 'A duplicated IAP request is sent.',
99
- masked: true
100
- },
101
- 20200 => {
102
- status: :retry,
103
- msg: 'Client should send the IAP request again.'
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
- end
110
+ })
107
111
 
108
- where keys are the supported codes and each value is an another hash to store the code information corresponding each codes. The code information hash contains the following keys:
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 "msg" method.
112
- * :masked - (optional)[Boolean] To define the error message is masked by default or not. The default value is false if no `:masked` is given.
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
- ### Raise a code-based error
119
+ #### Raise a code-based error
115
120
 
116
- Once you define the error_codes method, raising a code-based error is easy.
121
+ Once you setup the error codes, raising a code-based error is easy.
117
122
 
118
- raise MyError.new(20001)
123
+ raise MyError.gen(:purchase_info_incorrect)
119
124
 
120
- There are three argments in the new method, `MyError.new(code, msg, info)`. If the given code is defined in the error_codes, the error would contain the corresponding status and msg.
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.new(20001)
123
- e.code # 20001
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.(20001)"
130
+ e.msg # "Purchase information format is incorrect."
126
131
 
127
- You can give another string in "msg" arg to overwrite the original message:
132
+ You can give another string with the "msg" option to overwrite the original message:
128
133
 
129
- e = MyError.new(20001, "The another message which would override the original one.")
130
- e.code # 20001
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.(20001)"
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.new(1111)
137
- e.code # 1111
141
+ e = MyError.gen(:unknown_error)
142
+ e.code # :unknown_error
138
143
  e.status # :internal
139
- e.msg # "An internal error occurs.(1111)"
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.new("Invalid input!!")
145
- e.code # 99999
149
+ e = MyError.gen("Invalid input!!")
150
+ e.code # :internal_error
146
151
  e.status # :internal
147
- e.msg # "Invalid input!!"(99999)
152
+ e.msg # "Invalid input!!"
148
153
  e.internal? # true
149
154
 
150
- You can pass anything to the error handling you want through the "info" arg.
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.new(20001, nil, something)
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.new(:success)
159
- e.code # 0
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
- ### Handle the code-base error
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.new(20001, nil, 'some information')
178
- e.code # 20001
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.(20001)"
185
+ e.msg # "Purchase information format is incorrect."
181
186
  e.info # "some information"
182
- e.data # { code: 20001, status: :failed, msg: "Purchase information format is incorrect.(20001)", info: 'some information' }
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 override the `config` method in your code-based error to customize the default behavior. Here is an example:
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
- private
192
-
193
- def error_codes
196
+ error_codes({
194
197
  #...
195
- end
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 config hash contains the following keys:
216
+ where the configures are:
216
217
 
217
- * :success - Define the success code, status and message.
218
- * :internal - Define the internal error code, status and message.
219
- * :masked_msg - [String] Define the message to replace the masked one.
220
- * :code_in_msg - [:append/:prepend/:none] Define how to embed code in the message. The default option is :append.
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
- ### Mask the error message
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 "masked" arg in the "msg" or "data" method.
228
+ * Pass `masked: true` to the `msg` or `data` method.
227
229
 
228
- e = MyError.new(20001)
229
- e.msg(ture) # "An error occurs.(20001)"
230
- e.data(true) # { code: 20001, status: :failed, msg: "An error occurs.(20001)", info: {} }
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 `:masked` to the "msg" arg in the "new" method.
234
+ * Pass the `masked: true` to the `gen` method.
233
235
 
234
- e = MyError.new(20001, :masked)
235
- e.msg # "An error occurs.(20001)"
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
- The default masked message can be customized to your own message by overwriting the `:masked_msg` in your config hash. Please see [Configure your code-base error].
241
+ * Call `masked true` in your error class. Please see [Configure your code-base error].
240
242
 
241
- ## I18n
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
- We don't support the i18n for simpleness but you can do it yourself. Here is an example:
245
+ #### Show code in the error message
244
246
 
245
- class MyError < CodeError::Base
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
- private
248
-
249
- def error_codes
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
- def config
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
- def show_message(code, msg, masked = false)
293
- masked = masked || @masked || false
294
- m = masked ? I18n.t(config[:masked_msg]) : I18n.t(msg)
295
- code_in_msg = config[:code_in_msg]
296
- if code != config[:success][:code]
297
- if code_in_message == :append
298
- m = I18n.t('code_error.my_error.append_code_msg', msg: m, code: code)
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
- And the i18n yml file is:
263
+ e = MyError.gen(:purchase_info_incorrect, pos: :append)
264
+ e.msg # "Purchase information format is incorrect. (purchase_info_incorrect)"
307
265
 
308
- en:
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.