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 you can distinguish them easily.

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");
    };

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