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
+55Lines changed: 55 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.
@@ -172,3 +223,7 @@ end
172
223
> If `Retryable.with_context(:default)` fails after all retries, it will re-raise the last exception encountered.
173
224
174
225
See the source code at `lib/hooks/utils/retry.rb` for more details on how `Retryable.with_context(:default)` works.
226
+
227
+
### `#failbot` and `#stats`
228
+
229
+
The `failbot` and `stats` methods are available in all handler plugins. They are used to report errors and send statistics, respectively. These are custom methods and you can learn more about them in the [Instrumentation Plugins](instrument_plugins.md) documentation.
it"sends a POST request to the /webhooks/boomtown_with_error endpoint and it explodes"do
499
-
# TODO: Fix this acceptance test - the current error looks like this:
500
-
# 1) Hooks endpoints boomtown_with_error sends a POST request to the /webhooks/boomtown_with_error endpoint and it explodes
501
-
# Failure/Error: expect(response.body).to include(expected_body_content) if expected_body_content
502
-
#expected "{\"error\":\"server_error\",\"message\":\"undefined method 'error!' for an instance of BoomtownWithE...thread_pool.rb:167:in 'block in #Puma::ThreadPool#spawn_thread'\",\"handler\":\"BoomtownWithError\"}" to include "the payload triggered a boomtown error"
0 commit comments