Skip to content

Commit a7fa3a0

Browse files
committed
include content updates for Destination Functions
1 parent 88f110c commit a7fa3a0

File tree

1 file changed

+2
-89
lines changed

1 file changed

+2
-89
lines changed

src/connections/functions/destination-functions.md

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -81,58 +81,7 @@ To change which event type the handler listens to, you can rename it to the name
8181
8282
### Errors and error handling
8383

84-
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.
85-
86-
You can `throw` the following pre-defined error types to indicate that the function ran as expected, but that data was deliverable:
87-
88-
- `EventNotSupported`
89-
- `InvalidEventPayload`
90-
- `ValidationError`
91-
- `RetryError`
92-
93-
The examples show basic uses of these error types.
94-
95-
```js
96-
async function onGroup(event) {
97-
if (!event.traits.company) {
98-
throw new InvalidEventPayload('Company name is required')
99-
}
100-
}
101-
102-
async function onPage(event) {
103-
if (!event.properties.pageName) {
104-
throw new ValidationError('Page name is required')
105-
}
106-
}
107-
108-
async function onAlias(event) {
109-
throw new EventNotSupported('Alias event is not supported')
110-
}
111-
112-
async function onTrack(event) {
113-
let res
114-
try {
115-
res = await fetch('http://example-service.com/api', {
116-
method: 'POST',
117-
headers: {
118-
'Content-Type': 'application/json'
119-
},
120-
body: JSON.stringify({ event })
121-
})
122-
} catch (err) {
123-
// Retry on connection error
124-
throw new RetryError(err.message)
125-
}
126-
if (res.status >= 500 || res.status === 429) {
127-
// Retry on 5xx and 429s (ratelimits)
128-
throw new RetryError(`HTTP Status ${res.status}`)
129-
}
130-
}
131-
132-
```
133-
If you don't supply a function for an event type, Segment throws an `EventNotSupported` error by default.
134-
135-
You can read more about [error handling](#destination-functions-logs-and-errors) below.
84+
{% include content/functions/errors-and-error-handling.md %}
13685

13786
### Runtime and dependencies
13887

@@ -376,43 +325,7 @@ You can also choose to **Save & Deploy** to save the changes, and then choose wh
376325

377326
## Destination functions logs and errors
378327

379-
A function can throw errors, or Segment might encounter errors while invoking your function. You can view these errors in the [Event Delivery](/docs/connections/event-delivery/) tab for your Destination as in the example below.
380-
381-
![A screenshot of the event delivery tab, showing 519 failed events broken into categories explaining why they failed](images/event-delivery.png)
382-
383-
### Destination functions error types
384-
385-
- **Bad Request** - Any error thrown by the function code that is not covered by the other errors.
386-
- **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/){:target="_blank"}.
387-
- **Message Rejected** - Your code threw `InvalidEventPayload` or `ValidationError` due to invalid input.
388-
- **Unsupported Event Type** - Your code doesn't implement a specific event type (for example, `onTrack()`) or threw an `EventNotSupported` error.
389-
- **Retry** - Your code threw `RetryError` indicating that the function should be retried.
390-
391-
Segment only attempts to send the event to your destination function again if a **Retry** error occurs.
392-
393-
You can view Segment's list of [Integration Error Codes](/docs/connections/integration_error_codes/) for more information about what might cause an error.
394-
395-
### Destination functions logs
396-
397-
If your function throws an error, execution halts immediately. Segment captures the event, any outgoing requests/responses, any logs the function might have printed, as well as the error itself.
398-
399-
Segment then displays the captured error information in the [Event Delivery](/docs/connections/event-delivery/) page for your destination function. You can use this information to find and fix unexpected errors.
400-
401-
You can throw [an error or a custom error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error){:target="_blank"} and you can also add helpful context in logs using the [`console` API](https://developer.mozilla.org/en-US/docs/Web/API/console){:target="_blank"}. For example:
402-
403-
```js
404-
async function onTrack(event, settings) {
405-
const userId = event.userId
406-
407-
console.log('User ID is', userId)
408-
409-
if (typeof userId !== 'string' || userId.length < 8) {
410-
throw new ValidationError('User ID is invalid')
411-
}
412-
413-
console.log('User ID is valid')
414-
}
415-
```
328+
{% include content/functions/logs.md %}
416329

417330
> warning ""
418331
> **Warning:** Do not log sensitive data, such as personally-identifying information (PII), authentication tokens, or other secrets. Avoid logging entire request/response payloads. The **Function Logs** tab may be visible to other workspace members if they have the necessary permissions.

0 commit comments

Comments
 (0)