Skip to content

Commit a139452

Browse files
authored
Merge pull request #1055 from segmentio/function-retry-error
Update function docs for RetryError
2 parents 808104c + 6ff3b0a commit a139452

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/connections/functions/destination-functions.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ To change which type of event the handler listens to, you can rename it to the n
8686

8787
A function's execution is considered successful if it finishes without any errors. You can also `throw` an error to indicate a failure on purpose. You can use these errors to validate event data before processing it, to ensure your function works as expected.
8888

89-
There are three pre-defined error types that you can `throw` to indicate that the function ran as expected, but that data could not be delivered:
89+
You can `throw` the following pre-defined error types to indicate that the function ran as expected, but that data could not be delivered:
9090

9191
- `EventNotSupported`
9292
- `InvalidEventPayload`
9393
- `ValidationError`
94+
- `RetryError`
9495

9596
The examples show basic uses of these error types.
9697

@@ -110,6 +111,27 @@ async function onPage(event) {
110111
async function onAlias(event) {
111112
throw new EventNotSupported('Alias event is not supported')
112113
}
114+
115+
async function onTrack(event) {
116+
let res
117+
try {
118+
res = await fetch('http://example-service.com/api', {
119+
method: 'POST',
120+
headers: {
121+
'Content-Type': 'application/json'
122+
},
123+
body: JSON.stringify({ event })
124+
})
125+
} catch (err) {
126+
// Retry on connection error
127+
throw new RetryError(err.message)
128+
}
129+
if (res.status >= 500 || res.status === 429) {
130+
// Retry on 5xx and 429s (ratelimits)
131+
throw new RetryError(`HTTP Status ${res.status}`)
132+
}
133+
}
134+
113135
```
114136
If you do not supply a function for an event type, Segment throws an `EventNotSupported` error by default.
115137

@@ -186,8 +208,9 @@ A function can throw errors, or Segment might encounter errors while invoking yo
186208
- **Invalid Settings** - A configuration error prevented Segment from executing your code. If this error persists for more than an hour, [contact Segment Support](https://segment.com/help/contact/).
187209
- **Message Rejected** - Your code threw `InvalidEventPayload` or `ValidationError` due to invalid input.
188210
- **Unsupported Event Type** - Your code does not implement a specific event type (`onTrack()`, etc.) or threw a `EventNotSupported` error.
211+
- **Retry** - Your code threw `RetryError` indicating that the function should be retried.
189212

190-
When these errors occur, Segment does not attempt to send that event to your destination function again.
213+
Segment only attempts to send the event to your destination function again if a **Retry** error occurs.
191214

192215
### Destination functions logs
193216

src/connections/functions/source-functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,9 @@ async function onRequest(request, settings) {
350350
- **Invalid Settings**: A configuration error prevented Segment from executing your code. If this error persists for more than an hour, [contact Segment Support](https://segment.com/help/contact/).
351351
- **Message Rejected**: Your code threw `InvalidEventPayload` or `ValidationError` due to invalid input.
352352
- **Unsupported Event Type**: Your code does not implement a specific event type (`onTrack()`, etc.) or threw a `EventNotSupported` error.
353+
- **Retry** - Your code threw `RetryError` indicating that the function should be retried.
353354

354-
These errors are not retried.
355+
Segment only attempts to run your source function again if a **Retry** error occurs.
355356

356357
## Managing source functions
357358

0 commit comments

Comments
 (0)