Skip to main content
The BosonMail SDK uses a result-object error model. Every method resolves to an object that tells you whether the operation succeeded and, if not, why it failed. The constructor is the only place the SDK throws; everything else returns a result you inspect programmatically.

The result-object model

There are two result types:
  • Result from sendEmail
  • EmailStatusResult from getEmailStatus
Both share success, errorMessage, and statusCode. EmailStatusResult adds errorCode, which is useful for programmatic handling of status lookup failures.

Result fields

boolean
required
true for success, false for any failure.
string
Human-readable description of what went wrong.
number
HTTP status code from the API response. Absent for client-side validation errors and network/timeout failures.
string
Machine-readable error code. Present only on EmailStatusResult.

Error categories

1. Constructor errors (thrown)

The constructor throws Error with a descriptive message if any option is invalid. These are the only synchronous throws in the SDK.

2. Client-side validation (no statusCode)

Before any network request, the SDK validates input. These return { success: false, errorMessage } with no statusCode:

3. API errors (statusCode present)

When the API returns a non-2xx response or success !== true, the SDK extracts the error from the response body and returns:
The error message is resolved from body.error (string), body.error.message (object), body.message, or a fallback Request failed (HTTP <status> <statusText>).

4. Network and timeout (no statusCode)

When fetch fails or the request times out, the method resolves with:
Network errors (not timeouts) also lack statusCode and contain the underlying error message.
A timeout is ambiguous. The server may have accepted the email even though the client never received the response. Before retrying, check the email status if you have a messageId, or log the timeout for manual reconciliation. Blind retries risk duplicates.

Error codes on EmailStatusResult

The errorCode field helps you decide how to respond programmatically:

Classifying a Result

You can write a helper that categorizes any Result or EmailStatusResult:
classify-error.ts

Retry guidance

The SDK has no built-in retries. Implement retry logic in your application when it makes sense:
  • Retry: No statusCode due to a non-timeout network error, or statusCode >= 500, or statusCode === 429.
  • Do not retry: Client-side validation errors (statusCode absent and specific message), 4xx errors other than 429, timeouts (check status or log instead).
  • Timeout caution: sendEmail on timeout returns no messageId, so you cannot look up the status. Log the timeout and reconcile manually, or accept a duplicate risk explicitly before retrying.
retry-helper.ts
The helper above is application code, not an SDK feature. Adapt the retry strategy to your application’s reliability requirements and observability setup.

Converting results to exceptions

If you prefer a throw-style API, wrap the SDK calls:
throw-style.ts

JavaScript example

error-handling.mjs

Sending emails

Full sendEmail reference with all validation rules and error messages.

Email status

How getEmailStatus reports errors with errorCode values.

Client configuration

Timeout settings that affect network and timeout errors.

Guides: Production best practices

Logging, monitoring, and retry strategies for production deployments.