UNPKG

shopify-admin-api

Version:

Shopify Admin API is a NodeJS library built to help developers easily authenticate and make calls against the Shopify API. It was inspired by and borrows heavily from ShopifySharp.

59 lines (58 loc) 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShopifyError = void 0; function isType1(err) { return err.errors && typeof err.errors === 'string'; } function isType2(err) { return err.errors && typeof err.errors === 'object'; } function isType3(err) { return err.error && err.error_description; } class ShopifyError extends Error { constructor(response, body) { super(); this.body = body; /** * True when the requesting application has made too many requests and reached Shopify's API rate limit. */ this.apiRateLimitReached = false; this.errors = {}; this.callLimits = response.headers?.get('x-shopify-shop-api-call-limit'); this.statusCode = response.status; this.statusText = response.statusText; this.apiRateLimitReached = this.statusCode === 429 /* Too Many Requests */; this.message = `[Shopify Admin API] ${this.statusCode} ${this.statusText}. `; // Errors can be any of the following: // 1. { errors: "some error message"} // 2. { errors: { "order" : "some error message", "customer": [ "some error message" ] } } // 3. { error: "invalid_request", error_description:"The authorization code was not found or was already used" } if (isType1(body)) { this.errors['generic'] = [body.errors]; } else if (isType2(body)) { for (const property in body.errors) { const value = body.errors[property]; this.errors[property] = Array.isArray(value) ? value : [value]; } } else if (isType3(body)) { this.errors[body.error] = [body.error_description]; } } /** * @deprecated */ get isShopifyPrime() { return true; } /** * @deprecated */ get isShopifyAdminApi() { return true; } } exports.ShopifyError = ShopifyError; exports.default = ShopifyError;