1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.buildRequestError = exports.APIResponseError = exports.UnknownHTTPResponseError = exports.isHTTPResponseError = exports.RequestTimeoutError = exports.isNotionClientError = exports.ClientErrorCode = exports.APIErrorCode = void 0;
|
4 | const helpers_1 = require("./helpers");
|
5 |
|
6 |
|
7 |
|
8 | var APIErrorCode;
|
9 | (function (APIErrorCode) {
|
10 | APIErrorCode["Unauthorized"] = "unauthorized";
|
11 | APIErrorCode["RestrictedResource"] = "restricted_resource";
|
12 | APIErrorCode["ObjectNotFound"] = "object_not_found";
|
13 | APIErrorCode["RateLimited"] = "rate_limited";
|
14 | APIErrorCode["InvalidJSON"] = "invalid_json";
|
15 | APIErrorCode["InvalidRequestURL"] = "invalid_request_url";
|
16 | APIErrorCode["InvalidRequest"] = "invalid_request";
|
17 | APIErrorCode["ValidationError"] = "validation_error";
|
18 | APIErrorCode["ConflictError"] = "conflict_error";
|
19 | APIErrorCode["InternalServerError"] = "internal_server_error";
|
20 | APIErrorCode["ServiceUnavailable"] = "service_unavailable";
|
21 | })(APIErrorCode = exports.APIErrorCode || (exports.APIErrorCode = {}));
|
22 |
|
23 |
|
24 |
|
25 | var ClientErrorCode;
|
26 | (function (ClientErrorCode) {
|
27 | ClientErrorCode["RequestTimeout"] = "notionhq_client_request_timeout";
|
28 | ClientErrorCode["ResponseError"] = "notionhq_client_response_error";
|
29 | })(ClientErrorCode = exports.ClientErrorCode || (exports.ClientErrorCode = {}));
|
30 |
|
31 |
|
32 |
|
33 | class NotionClientErrorBase extends Error {
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function isNotionClientError(error) {
|
40 | return helpers_1.isObject(error) && error instanceof NotionClientErrorBase;
|
41 | }
|
42 | exports.isNotionClientError = isNotionClientError;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function isNotionClientErrorWithCode(error, codes) {
|
50 | return isNotionClientError(error) && error.code in codes;
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | class RequestTimeoutError extends NotionClientErrorBase {
|
56 | constructor(message = "Request to Notion API has timed out") {
|
57 | super(message);
|
58 | this.code = ClientErrorCode.RequestTimeout;
|
59 | this.name = "RequestTimeoutError";
|
60 | }
|
61 | static isRequestTimeoutError(error) {
|
62 | return isNotionClientErrorWithCode(error, {
|
63 | [ClientErrorCode.RequestTimeout]: true,
|
64 | });
|
65 | }
|
66 | static rejectAfterTimeout(promise, timeoutMS) {
|
67 | return new Promise((resolve, reject) => {
|
68 | const timeoutId = setTimeout(() => {
|
69 | reject(new RequestTimeoutError());
|
70 | }, timeoutMS);
|
71 | promise
|
72 | .then(resolve)
|
73 | .catch(reject)
|
74 | .then(() => clearTimeout(timeoutId));
|
75 | });
|
76 | }
|
77 | }
|
78 | exports.RequestTimeoutError = RequestTimeoutError;
|
79 | class HTTPResponseError extends NotionClientErrorBase {
|
80 | constructor(args) {
|
81 | super(args.message);
|
82 | this.name = "HTTPResponseError";
|
83 | const { code, status, headers, rawBodyText } = args;
|
84 | this.code = code;
|
85 | this.status = status;
|
86 | this.headers = headers;
|
87 | this.body = rawBodyText;
|
88 | }
|
89 | }
|
90 | const httpResponseErrorCodes = {
|
91 | [ClientErrorCode.ResponseError]: true,
|
92 | [APIErrorCode.Unauthorized]: true,
|
93 | [APIErrorCode.RestrictedResource]: true,
|
94 | [APIErrorCode.ObjectNotFound]: true,
|
95 | [APIErrorCode.RateLimited]: true,
|
96 | [APIErrorCode.InvalidJSON]: true,
|
97 | [APIErrorCode.InvalidRequestURL]: true,
|
98 | [APIErrorCode.InvalidRequest]: true,
|
99 | [APIErrorCode.ValidationError]: true,
|
100 | [APIErrorCode.ConflictError]: true,
|
101 | [APIErrorCode.InternalServerError]: true,
|
102 | [APIErrorCode.ServiceUnavailable]: true,
|
103 | };
|
104 | function isHTTPResponseError(error) {
|
105 | if (!isNotionClientErrorWithCode(error, httpResponseErrorCodes)) {
|
106 | return false;
|
107 | }
|
108 | return true;
|
109 | }
|
110 | exports.isHTTPResponseError = isHTTPResponseError;
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | class UnknownHTTPResponseError extends HTTPResponseError {
|
116 | constructor(args) {
|
117 | var _a;
|
118 | super({
|
119 | ...args,
|
120 | code: ClientErrorCode.ResponseError,
|
121 | message: (_a = args.message) !== null && _a !== void 0 ? _a : `Request to Notion API failed with status: ${args.status}`,
|
122 | });
|
123 | this.name = "UnknownHTTPResponseError";
|
124 | }
|
125 | static isUnknownHTTPResponseError(error) {
|
126 | return isNotionClientErrorWithCode(error, {
|
127 | [ClientErrorCode.ResponseError]: true,
|
128 | });
|
129 | }
|
130 | }
|
131 | exports.UnknownHTTPResponseError = UnknownHTTPResponseError;
|
132 | const apiErrorCodes = {
|
133 | [APIErrorCode.Unauthorized]: true,
|
134 | [APIErrorCode.RestrictedResource]: true,
|
135 | [APIErrorCode.ObjectNotFound]: true,
|
136 | [APIErrorCode.RateLimited]: true,
|
137 | [APIErrorCode.InvalidJSON]: true,
|
138 | [APIErrorCode.InvalidRequestURL]: true,
|
139 | [APIErrorCode.InvalidRequest]: true,
|
140 | [APIErrorCode.ValidationError]: true,
|
141 | [APIErrorCode.ConflictError]: true,
|
142 | [APIErrorCode.InternalServerError]: true,
|
143 | [APIErrorCode.ServiceUnavailable]: true,
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | class APIResponseError extends HTTPResponseError {
|
150 | constructor() {
|
151 | super(...arguments);
|
152 | this.name = "APIResponseError";
|
153 | }
|
154 | static isAPIResponseError(error) {
|
155 | return isNotionClientErrorWithCode(error, apiErrorCodes);
|
156 | }
|
157 | }
|
158 | exports.APIResponseError = APIResponseError;
|
159 | function buildRequestError(response, bodyText) {
|
160 | const apiErrorResponseBody = parseAPIErrorResponseBody(bodyText);
|
161 | if (apiErrorResponseBody !== undefined) {
|
162 | return new APIResponseError({
|
163 | code: apiErrorResponseBody.code,
|
164 | message: apiErrorResponseBody.message,
|
165 | headers: response.headers,
|
166 | status: response.status,
|
167 | rawBodyText: bodyText,
|
168 | });
|
169 | }
|
170 | return new UnknownHTTPResponseError({
|
171 | message: undefined,
|
172 | headers: response.headers,
|
173 | status: response.status,
|
174 | rawBodyText: bodyText,
|
175 | });
|
176 | }
|
177 | exports.buildRequestError = buildRequestError;
|
178 | function parseAPIErrorResponseBody(body) {
|
179 | if (typeof body !== "string") {
|
180 | return;
|
181 | }
|
182 | let parsed;
|
183 | try {
|
184 | parsed = JSON.parse(body);
|
185 | }
|
186 | catch (parseError) {
|
187 | return;
|
188 | }
|
189 | if (!helpers_1.isObject(parsed) ||
|
190 | typeof parsed["message"] !== "string" ||
|
191 | !isAPIErrorCode(parsed["code"])) {
|
192 | return;
|
193 | }
|
194 | return {
|
195 | ...parsed,
|
196 | code: parsed["code"],
|
197 | message: parsed["message"],
|
198 | };
|
199 | }
|
200 | function isAPIErrorCode(code) {
|
201 | return typeof code === "string" && code in apiErrorCodes;
|
202 | }
|
203 |
|
\ | No newline at end of file |