You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+119-7Lines changed: 119 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,103 @@ ErrorResponse.configure do |config|
44
44
end
45
45
```
46
46
47
+
### Error Message Resolver
48
+
49
+
You can provide an extension hook to customize the message passed into `ErrorResponse::Helper#error_response` without monkey patching.
50
+
51
+
When `error_response` is called, the gem invokes `ErrorResponse.resolve_error_message` before building the API payload. The resolved value is then passed to `ErrorResponse.to_api` as the optional message suffix.
52
+
53
+
```ruby
54
+
ErrorResponse.configure do |config|
55
+
config.error_message_resolver = lambda do |key:, error_message:, error_data:, context:|
56
+
# Return a string message, nil, or any value accepted by `to_api`.
57
+
# context is the current controller instance when called from helper.
58
+
"customized message for #{key}"
59
+
end
60
+
end
61
+
```
62
+
63
+
#### Resolver arguments
64
+
65
+
| Argument | Description |
66
+
| --- | --- |
67
+
|`key`| The error key passed to `error_response` (e.g. `:bad_request_1`). |
68
+
|`error_message`| The optional custom message passed to `error_response`. |
69
+
|`error_data`| The optional hash or array passed to `error_response`. |
70
+
|`context`| The current controller instance when called from `ErrorResponse::Helper`. `nil` when calling `ErrorResponse.resolve_error_message` directly. |
71
+
72
+
#### Return value
73
+
74
+
- Return a `String` to append after the YAML-defined message (e.g. `"bad request 1: customized message for bad_request_1"`).
75
+
- Return `nil` to keep only the YAML-defined message (no suffix is appended).
76
+
- If the resolver is not configured, does not respond to `call`, or raises an exception, the original `error_message` is used as a safe fallback.
"error_message": "bad request 1: no required data",
137
+
"error_key": "bad_request_1",
138
+
"a": 1
139
+
}
140
+
```
141
+
142
+
The resolver can rewrite `error_message` before it is appended. `error_data` is merged into the JSON response after message resolution and is not modified by the resolver.
143
+
47
144
48
145
## Usage
49
146
@@ -68,7 +165,7 @@ return success_response(data) if success?
68
165
```
69
166
70
167
> response status: 200
71
-
>
168
+
>
72
169
> response body:
73
170
74
171
```json
@@ -90,7 +187,7 @@ return error_response(:bad_request_1) if failed?
90
187
```
91
188
92
189
> response status: 400
93
-
>
190
+
>
94
191
> response body:
95
192
96
193
```json
@@ -103,13 +200,15 @@ return error_response(:bad_request_1) if failed?
103
200
104
201
You can also provide your custom error message and error data. If error data is a hash, it will be merged into the json response; If it is an array, it will be merged into the json response with an `error_data` key.
105
202
203
+
When `config.error_message_resolver` is configured, the custom message is passed through the resolver before being appended to the YAML-defined message. See [Error Message Resolver](#error-message-resolver) for details.
0 commit comments