Skip to content

Commit 74451a5

Browse files
committed
Check the HTTP status
1 parent 1bb2edf commit 74451a5

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/connections/functions/destination-functions.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,25 @@ async function onAlias(event) {
113113
}
114114

115115
async function onTrack(event) {
116+
let res
116117
try {
117-
await fetch('http://example-service.com/api', {
118+
res = await fetch('http://example-service.com/api', {
118119
method: 'POST',
119120
headers: {
120121
'Content-Type': 'application/json'
121-
}
122+
},
122123
body: JSON.stringify({ event })
123124
})
124125
} catch (err) {
126+
// Retry on connection error
125127
throw new RetryError(err.message)
126128
}
129+
if (res.status >= 500 || res.status === 429) {
130+
// Retry on 5xx and 429s (ratelimits)
131+
throw new RetryError(`HTTP Status ${res.status}`)
132+
}
127133
}
134+
128135
```
129136
If you do not supply a function for an event type, Segment throws an `EventNotSupported` error by default.
130137

0 commit comments

Comments
 (0)