Improves error handling for SMS sending (#633)

This commit is contained in:
Chris Anderson 2025-02-26 10:46:14 -06:00 committed by GitHub
parent 4d1d7d1e53
commit 0540af3ca9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 10 deletions

View file

@ -79,14 +79,14 @@ export default class TelnyxTextProvider extends TextProvider {
// https://support.telnyx.com/en/articles/6505121-telnyx-messaging-error-codes
const error = responseBody.errors?.[0]
if (error.code === 40300) {
if (error?.code === 40300) {
// Unable to send because recipient has unsubscribed
throw new UnsubscribeTextError(this.type, this.phone_number, responseBody.message)
} else if (responseBody.code === 40008) {
throw new UnsubscribeTextError(this.type, to, error.title)
} else if (error?.code === 40008) {
// Unable to send because region is not enabled
throw new UndeliverableTextError(this.type, this.phone_number, responseBody.message)
throw new UndeliverableTextError(this.type, to, error.title)
}
throw new TextError(this.type, this.phone_number, responseBody.message)
throw new TextError(this.type, to, error?.title, responseBody)
}
}

View file

@ -2,10 +2,10 @@ import { ContextError } from '../../error/ErrorHandler'
export default class TextError extends ContextError {
phone: string
constructor(type: string, phone: string, message: string) {
constructor(type: string, phone: string, message: string, context: any = {}) {
super(`Text Error: ${message}`)
this.phone = phone
this.context = { phone, type }
this.context = { phone, type, ...context }
Error.captureStackTrace(this, TextError)
}
}

View file

@ -83,12 +83,12 @@ export default class TwilioTextProvider extends TextProvider {
} else {
if (responseBody.code === 21610) {
// Unable to send because recipient has unsubscribed
throw new UnsubscribeTextError(this.type, this.phone_number, responseBody.message)
throw new UnsubscribeTextError(this.type, to, responseBody.message)
} else if (responseBody.code === 21408) {
// Unable to send because region is not enabled
throw new UndeliverableTextError(this.type, this.phone_number, responseBody.message)
throw new UndeliverableTextError(this.type, to, responseBody.message)
}
throw new TextError(this.type, this.phone_number, responseBody.message)
throw new TextError(this.type, to, responseBody.message)
}
}