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: docs/handler_plugins.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -140,6 +140,57 @@ This section goes into details on the built-in features that exist in all handle
140
140
141
141
The `log.debug`, `log.info`, `log.warn`, and `log.error` methods are available in all handler plugins. They are used to log messages at different levels of severity.
142
142
143
+
### `#error!`
144
+
145
+
All handler plugins have access to the `error!` method, which is used to raise an error with a specific message and HTTP status code. This is useful for returning error responses to the webhook sender.
146
+
147
+
```ruby
148
+
classExample < Hooks::Plugins::Handlers::Base
149
+
# Example webhook handler
150
+
#
151
+
#@parampayload[Hash, String] Webhook payload
152
+
#@paramheaders[Hash<String, String>] HTTP headers
153
+
#@paramenv[Hash] A modified Rack environment that contains a lot of context about the request
154
+
#@paramconfig[Hash] Endpoint configuration
155
+
#@return[Hash] Response data
156
+
defcall(payload:, headers:, env:, config:)
157
+
158
+
if payload.nil? || payload.empty?
159
+
log.error("Payload is empty or nil")
160
+
error!("Payload cannot be empty or nil", 400)
161
+
end
162
+
163
+
return {
164
+
status:"success"
165
+
}
166
+
end
167
+
end
168
+
```
169
+
170
+
You can also use the `error!` method to return a JSON response as well:
171
+
172
+
```ruby
173
+
classExample < Hooks::Plugins::Handlers::Base
174
+
defcall(payload:, headers:, env:, config:)
175
+
176
+
if payload.nil? || payload.empty?
177
+
log.error("Payload is empty or nil")
178
+
error!({
179
+
error:"payload_empty",
180
+
message:"the payload cannot be empty or nil",
181
+
success:false,
182
+
custom_value:"some_custom_value",
183
+
request_id: env["hooks.request_id"]
184
+
}, 500)
185
+
end
186
+
187
+
return {
188
+
status:"success"
189
+
}
190
+
end
191
+
end
192
+
```
193
+
143
194
### `#Retryable.with_context(:default)`
144
195
145
196
This method uses a default `Retryable` context to handle retries. It is used to wrap the execution of a block of code that may need to be retried in case of failure.
0 commit comments