UNPKG

7.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.buildRequestError = exports.APIResponseError = exports.UnknownHTTPResponseError = exports.isHTTPResponseError = exports.RequestTimeoutError = exports.isNotionClientError = exports.ClientErrorCode = exports.APIErrorCode = void 0;
4const helpers_1 = require("./helpers");
5/**
6 * Error codes returned in responses from the API.
7 */
8var 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 * Error codes generated for client errors.
24 */
25var 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 * Base error type.
32 */
33class NotionClientErrorBase extends Error {
34}
35/**
36 * @param error any value, usually a caught error.
37 * @returns `true` if error is a `NotionClientError`.
38 */
39function isNotionClientError(error) {
40 return helpers_1.isObject(error) && error instanceof NotionClientErrorBase;
41}
42exports.isNotionClientError = isNotionClientError;
43/**
44 * Narrows down the types of a NotionClientError.
45 * @param error any value, usually a caught error.
46 * @param codes an object mapping from possible error codes to `true`
47 * @returns `true` if error is a `NotionClientError` with a code in `codes`.
48 */
49function isNotionClientErrorWithCode(error, codes) {
50 return isNotionClientError(error) && error.code in codes;
51}
52/**
53 * Error thrown by the client if a request times out.
54 */
55class 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}
78exports.RequestTimeoutError = RequestTimeoutError;
79class 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}
90const 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};
104function isHTTPResponseError(error) {
105 if (!isNotionClientErrorWithCode(error, httpResponseErrorCodes)) {
106 return false;
107 }
108 return true;
109}
110exports.isHTTPResponseError = isHTTPResponseError;
111/**
112 * Error thrown if an API call responds with an unknown error code, or does not respond with
113 * a property-formatted error.
114 */
115class 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}
131exports.UnknownHTTPResponseError = UnknownHTTPResponseError;
132const 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 * A response from the API indicating a problem.
147 * Use the `code` property to handle various kinds of errors. All its possible values are in `APIErrorCode`.
148 */
149class APIResponseError extends HTTPResponseError {
150 constructor() {
151 super(...arguments);
152 this.name = "APIResponseError";
153 }
154 static isAPIResponseError(error) {
155 return isNotionClientErrorWithCode(error, apiErrorCodes);
156 }
157}
158exports.APIResponseError = APIResponseError;
159function 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}
177exports.buildRequestError = buildRequestError;
178function 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}
200function isAPIErrorCode(code) {
201 return typeof code === "string" && code in apiErrorCodes;
202}
203//# sourceMappingURL=errors.js.map
\No newline at end of file