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: src/connections/functions/destination-functions.md
+2-89Lines changed: 2 additions & 89 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,58 +81,7 @@ To change which event type the handler listens to, you can rename it to the name
81
81
82
82
### Errors and error handling
83
83
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
-
asyncfunctiononGroup(event) {
97
-
if (!event.traits.company) {
98
-
thrownewInvalidEventPayload('Company name is required')
99
-
}
100
-
}
101
-
102
-
asyncfunctiononPage(event) {
103
-
if (!event.properties.pageName) {
104
-
thrownewValidationError('Page name is required')
105
-
}
106
-
}
107
-
108
-
asyncfunctiononAlias(event) {
109
-
thrownewEventNotSupported('Alias event is not supported')
110
-
}
111
-
112
-
asyncfunctiononTrack(event) {
113
-
let res
114
-
try {
115
-
res =awaitfetch('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
-
thrownewRetryError(err.message)
125
-
}
126
-
if (res.status>=500||res.status===429) {
127
-
// Retry on 5xx and 429s (ratelimits)
128
-
thrownewRetryError(`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 %}
136
85
137
86
### Runtime and dependencies
138
87
@@ -376,43 +325,7 @@ You can also choose to **Save & Deploy** to save the changes, and then choose wh
376
325
377
326
## Destination functions logs and errors
378
327
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
-

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
-
asyncfunctiononTrack(event, settings) {
405
-
constuserId=event.userId
406
-
407
-
console.log('User ID is', userId)
408
-
409
-
if (typeof userId !=='string'||userId.length<8) {
410
-
thrownewValidationError('User ID is invalid')
411
-
}
412
-
413
-
console.log('User ID is valid')
414
-
}
415
-
```
328
+
{% include content/functions/logs.md %}
416
329
417
330
> warning ""
418
331
> **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