UNPKG

@scaleway/sdk-client

Version:
44 lines (43 loc) 1.66 kB
import { isJSONObject } from "../../../helpers/json.js"; import { ScalewayError } from "../scw-error.js"; const buildMessage = (list) => `quota(s) exceeded: ${list.map((details) => { const message = `Quotas reached: You have reached the maximum number of ${details.resource} authorized by your Organization. Access the quotas page from your Organization dashboard to manage quotas.`; return details.scope ? `${message} for ${details.scope.kind} '${details.scope.id}'` : message; }).join("; ")}`; const buildScope = (detail) => { if (typeof detail.organization_id === "string" && detail.organization_id.length) { return { id: detail.organization_id, kind: "organization" }; } if (typeof detail.project_id === "string" && detail.project_id.length) { return { id: detail.project_id, kind: "project" }; } return void 0; }; class QuotasExceededError extends ScalewayError { constructor(status, body, list) { super(status, body, buildMessage(list)); this.status = status; this.body = body; this.list = list; this.name = "QuotasExceededError"; } static fromJSON(status, obj) { if (!Array.isArray(obj.details)) return null; return new QuotasExceededError( status, obj, obj.details.reduce( (list, detail) => isJSONObject(detail) && typeof detail.resource === "string" && typeof detail.quota === "number" && typeof detail.current === "number" ? list.concat({ current: detail.current, quota: detail.quota, resource: detail.resource, scope: buildScope(detail) }) : list, [] ) ); } } export { QuotasExceededError };