Skip to content

Commit d06ecf5

Browse files
committed
docs
1 parent 2760d30 commit d06ecf5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

docs/handler_plugins.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,57 @@ This section goes into details on the built-in features that exist in all handle
140140

141141
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.
142142

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+
class Example < Hooks::Plugins::Handlers::Base
149+
# Example webhook handler
150+
#
151+
# @param payload [Hash, String] Webhook payload
152+
# @param headers [Hash<String, String>] HTTP headers
153+
# @param env [Hash] A modified Rack environment that contains a lot of context about the request
154+
# @param config [Hash] Endpoint configuration
155+
# @return [Hash] Response data
156+
def call(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+
class Example < Hooks::Plugins::Handlers::Base
174+
def call(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+
143194
### `#Retryable.with_context(:default)`
144195

145196
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

Comments
 (0)