Skip to content

Error Handling

Igor Balos edited this page Oct 29, 2018 · 5 revisions

We added a custom error handler to the library, so that you can handle errors easier. You can handler errors two ways, through callbacks and through promises.

Let's check out both.

Handling errors through promises

This is the way we recommend handling errors. Each error will contain things like name, message, statusCode so that you can distinguish them easily.

Check out the following code example how error could be handled.

let postmark = require("postmark")
const accountToken = "xxxx-xxxxx-xxxx-xxxxx-xxxxxx"
let accountClient = new postmark.AccountClient(accountToken);

accountClient.getDomain(1).then(result => {
    console.log(result);
}).catch(error => {
    if (error instanceof postmark.Errors.UnknownError) {
        console.log("Unkown Postmark error");
        console.error(error)
    };

    if (error.name === 'ApiInputError') {
        console.log(error.name);
    }
});

Handling errors through callbacks

Logic used with callbacks would be similar.

accountClient.getDomain(1, function (error, data) {
    if (error instanceof postmark.Errors.UnknownError) {
        console.log("Unkown Postmark error");
        console.error(error)
    }

    if (error.name === 'ApiInputError') {
        console.log(error.name);
    }
});

Clone this wiki locally