Skip to content

Commit 824fa8e

Browse files
author
markzegarelli
authored
Merge branch 'master' into develop
2 parents 3f9bb3a + 20c4035 commit 824fa8e

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/_includes/content/functions/errors-and-error-handling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Use for destination and insert functions -->
1+
<!-- Use for destination functions -->
22

33
Segment considers a function's execution successful if it finishes without error. You can also `throw` an error to create a failure on purpose. Use these errors to validate event data before processing it, to ensure the function works as expected.
44

src/connections/functions/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ Learn more about [destination functions](/docs/connections/functions/destination
3535
#### Destination insert functions
3636
Destination insert functions help you enrich your data with code before you send it to downstream destinations.
3737

38-
> info "Destination Insert Functions in Private Beta"
39-
> Destination Insert Functions is in Private Beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. [Contact Segment](https://segment.com/help/contact/){:target="_blank"} with any feedback or questions.
38+
> info "Destination Insert Functions in Public Beta"
39+
> Destination Insert Functions is in Public Beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. [Contact Segment](https://segment.com/help/contact/){:target="_blank"} with any feedback or questions.
4040
41-
Use cases:
41+
Use cases:
4242
- Implement custom logic and enrich data with third party sources
4343
- Transform outgoing data with advanced filtration and computation
4444
- Ensure data compliance by performing tokenisation, encryption, or decryption before sending data downstream

src/connections/functions/insert-functions.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Use Destination Insert Functions to enrich, transform, or filter your data befor
1414

1515
**Customize filtration for your destinations**: Create custom logic with nested if-else statements, regex, custom business rules, and more to filter event data.
1616

17-
> info "Destination Insert Functions in Private Beta"
18-
> Destination Insert Functions is in Private Beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. [Contact Segment](https://segment.com/help/contact/){:target="_blank"} with any feedback or questions.
17+
> info "Destination Insert Functions in Public Beta"
18+
> Destination Insert Functions is in Public Beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. [Contact Segment](https://segment.com/help/contact/){:target="_blank"} with any feedback or questions.
1919
2020

2121
## Create destination insert functions
@@ -91,6 +91,9 @@ async function onTrack(event) {
9191
timestamp: event.timestamp
9292
})
9393
})
94+
95+
return event;
96+
9497
}
9598
```
9699

@@ -101,7 +104,60 @@ To change which event type the handler listens to, you can rename it to the name
101104
102105
### Errors and error handling
103106

104-
{% include content/functions/errors-and-error-handling.md %}
107+
Segment considers a function's execution successful if it finishes without error. You can `throw` an error to create a failure on purpose. Use these errors to validate event data before processing it to ensure the function works as expected.
108+
109+
You can `throw` the following pre-defined error types to indicate that the function ran as expected, but the data was not deliverable:
110+
111+
- `EventNotSupported`
112+
- `InvalidEventPayload`
113+
- `ValidationError`
114+
- `RetryError`
115+
116+
The examples show basic uses of these error types.
117+
118+
```js
119+
async function onGroup(event) {
120+
if (!event.traits.company) {
121+
throw new InvalidEventPayload('Company name is required')
122+
}
123+
}
124+
125+
async function onPage(event) {
126+
if (!event.properties.pageName) {
127+
throw new ValidationError('Page name is required')
128+
}
129+
}
130+
131+
async function onAlias(event) {
132+
throw new EventNotSupported('Alias event is not supported')
133+
}
134+
135+
async function onTrack(event) {
136+
let res
137+
try {
138+
res = await fetch('http://example-service.com/api', {
139+
method: 'POST',
140+
headers: {
141+
'Content-Type': 'application/json'
142+
},
143+
body: JSON.stringify({ event })
144+
})
145+
146+
return event;
147+
148+
} catch (err) {
149+
// Retry on connection error
150+
throw new RetryError(err.message)
151+
}
152+
if (res.status >= 500 || res.status === 429) {
153+
// Retry on 5xx and 429s (ratelimits)
154+
throw new RetryError(`HTTP Status ${res.status}`)
155+
}
156+
}
157+
158+
```
159+
If you don't supply a function for an event type, Segment throws an `EventNotSupported` error by default.
160+
105161

106162
You can read more about [error handling](#destination-insert-functions-logs-and-errors) below.
107163

0 commit comments

Comments
 (0)