@databricks/sdk-core 0.0.0-dev → 0.1.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/LICENSE +203 -0
  2. package/README.md +11 -1
  3. package/dist/api/execute.d.ts +12 -0
  4. package/dist/api/execute.d.ts.map +1 -0
  5. package/dist/api/execute.js +77 -0
  6. package/dist/api/execute.js.map +1 -0
  7. package/dist/api/index.d.ts +12 -0
  8. package/dist/api/index.d.ts.map +1 -0
  9. package/dist/api/index.js +8 -0
  10. package/dist/api/index.js.map +1 -0
  11. package/dist/api/limiter.d.ts +9 -0
  12. package/dist/api/limiter.d.ts.map +1 -0
  13. package/dist/api/limiter.js +2 -0
  14. package/dist/api/limiter.js.map +1 -0
  15. package/dist/api/options.d.ts +22 -0
  16. package/dist/api/options.d.ts.map +1 -0
  17. package/dist/api/options.js +2 -0
  18. package/dist/api/options.js.map +1 -0
  19. package/dist/api/retrier.d.ts +58 -0
  20. package/dist/api/retrier.d.ts.map +1 -0
  21. package/dist/api/retrier.js +70 -0
  22. package/dist/api/retrier.js.map +1 -0
  23. package/dist/apierror/apierror.d.ts +53 -0
  24. package/dist/apierror/apierror.d.ts.map +1 -0
  25. package/dist/apierror/apierror.js +217 -0
  26. package/dist/apierror/apierror.js.map +1 -0
  27. package/dist/apierror/codes/codes.d.ts +140 -0
  28. package/dist/apierror/codes/codes.d.ts.map +1 -0
  29. package/dist/apierror/codes/codes.js +167 -0
  30. package/dist/apierror/codes/codes.js.map +1 -0
  31. package/dist/apierror/codes/index.d.ts +7 -0
  32. package/dist/apierror/codes/index.d.ts.map +1 -0
  33. package/dist/apierror/codes/index.js +7 -0
  34. package/dist/apierror/codes/index.js.map +1 -0
  35. package/dist/apierror/details.d.ts +177 -0
  36. package/dist/apierror/details.d.ts.map +1 -0
  37. package/dist/apierror/details.js +250 -0
  38. package/dist/apierror/details.js.map +1 -0
  39. package/dist/apierror/index.d.ts +8 -0
  40. package/dist/apierror/index.d.ts.map +1 -0
  41. package/dist/apierror/index.js +7 -0
  42. package/dist/apierror/index.js.map +1 -0
  43. package/dist/clientinfo/agent.d.ts +56 -0
  44. package/dist/clientinfo/agent.d.ts.map +1 -0
  45. package/dist/clientinfo/agent.js +114 -0
  46. package/dist/clientinfo/agent.js.map +1 -0
  47. package/dist/clientinfo/base.d.ts +39 -0
  48. package/dist/clientinfo/base.d.ts.map +1 -0
  49. package/dist/clientinfo/base.js +62 -0
  50. package/dist/clientinfo/base.js.map +1 -0
  51. package/dist/clientinfo/clientinfo.d.ts +61 -0
  52. package/dist/clientinfo/clientinfo.d.ts.map +1 -0
  53. package/dist/clientinfo/clientinfo.js +96 -0
  54. package/dist/clientinfo/clientinfo.js.map +1 -0
  55. package/dist/clientinfo/default.browser.d.ts +17 -0
  56. package/dist/clientinfo/default.browser.d.ts.map +1 -0
  57. package/dist/clientinfo/default.browser.js +20 -0
  58. package/dist/clientinfo/default.browser.js.map +1 -0
  59. package/dist/clientinfo/default.d.ts +14 -0
  60. package/dist/clientinfo/default.d.ts.map +1 -0
  61. package/dist/clientinfo/default.js +104 -0
  62. package/dist/clientinfo/default.js.map +1 -0
  63. package/dist/clientinfo/index.browser.d.ts +5 -0
  64. package/dist/clientinfo/index.browser.d.ts.map +1 -0
  65. package/dist/clientinfo/index.browser.js +4 -0
  66. package/dist/clientinfo/index.browser.js.map +1 -0
  67. package/dist/clientinfo/index.d.ts +5 -0
  68. package/dist/clientinfo/index.d.ts.map +1 -0
  69. package/dist/clientinfo/index.js +4 -0
  70. package/dist/clientinfo/index.js.map +1 -0
  71. package/dist/http/http.d.ts +40 -0
  72. package/dist/http/http.d.ts.map +1 -0
  73. package/dist/http/http.js +37 -0
  74. package/dist/http/http.js.map +1 -0
  75. package/dist/http/index.d.ts +8 -0
  76. package/dist/http/index.d.ts.map +1 -0
  77. package/dist/http/index.js +7 -0
  78. package/dist/http/index.js.map +1 -0
  79. package/dist/index.d.ts +6 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +7 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/logger/index.d.ts +8 -0
  84. package/dist/logger/index.d.ts.map +1 -0
  85. package/dist/logger/index.js +7 -0
  86. package/dist/logger/index.js.map +1 -0
  87. package/dist/logger/logger.d.ts +49 -0
  88. package/dist/logger/logger.d.ts.map +1 -0
  89. package/dist/logger/logger.js +65 -0
  90. package/dist/logger/logger.js.map +1 -0
  91. package/dist/profiles/errors.d.ts +17 -0
  92. package/dist/profiles/errors.d.ts.map +1 -0
  93. package/dist/profiles/errors.js +19 -0
  94. package/dist/profiles/errors.js.map +1 -0
  95. package/dist/profiles/index.browser.d.ts +10 -0
  96. package/dist/profiles/index.browser.d.ts.map +1 -0
  97. package/dist/profiles/index.browser.js +8 -0
  98. package/dist/profiles/index.browser.js.map +1 -0
  99. package/dist/profiles/index.d.ts +15 -0
  100. package/dist/profiles/index.d.ts.map +1 -0
  101. package/dist/profiles/index.js +13 -0
  102. package/dist/profiles/index.js.map +1 -0
  103. package/dist/profiles/ini.d.ts +36 -0
  104. package/dist/profiles/ini.d.ts.map +1 -0
  105. package/dist/profiles/ini.js +113 -0
  106. package/dist/profiles/ini.js.map +1 -0
  107. package/dist/profiles/profile.d.ts +131 -0
  108. package/dist/profiles/profile.d.ts.map +1 -0
  109. package/dist/profiles/profile.js +307 -0
  110. package/dist/profiles/profile.js.map +1 -0
  111. package/dist/profiles/resolve.d.ts +29 -0
  112. package/dist/profiles/resolve.d.ts.map +1 -0
  113. package/dist/profiles/resolve.js +206 -0
  114. package/dist/profiles/resolve.js.map +1 -0
  115. package/dist/profiles/secret.d.ts +25 -0
  116. package/dist/profiles/secret.d.ts.map +1 -0
  117. package/dist/profiles/secret.js +38 -0
  118. package/dist/profiles/secret.js.map +1 -0
  119. package/dist/wkt/fieldmask.d.ts +32 -0
  120. package/dist/wkt/fieldmask.d.ts.map +1 -0
  121. package/dist/wkt/fieldmask.js +68 -0
  122. package/dist/wkt/fieldmask.js.map +1 -0
  123. package/dist/wkt/index.d.ts +4 -0
  124. package/dist/wkt/index.d.ts.map +1 -0
  125. package/dist/wkt/index.js +2 -0
  126. package/dist/wkt/index.js.map +1 -0
  127. package/dist/wkt/value.d.ts +13 -0
  128. package/dist/wkt/value.d.ts.map +1 -0
  129. package/dist/wkt/value.js +2 -0
  130. package/dist/wkt/value.js.map +1 -0
  131. package/package.json +82 -4
  132. package/src/api/execute.ts +102 -0
  133. package/src/api/index.ts +12 -0
  134. package/src/api/limiter.ts +8 -0
  135. package/src/api/options.ts +22 -0
  136. package/src/api/retrier.ts +108 -0
  137. package/src/apierror/apierror.ts +253 -0
  138. package/src/apierror/codes/codes.ts +189 -0
  139. package/src/apierror/codes/index.ts +7 -0
  140. package/src/apierror/details.ts +459 -0
  141. package/src/apierror/index.ts +24 -0
  142. package/src/clientinfo/agent.ts +125 -0
  143. package/src/clientinfo/base.ts +73 -0
  144. package/src/clientinfo/clientinfo.ts +129 -0
  145. package/src/clientinfo/default.browser.ts +24 -0
  146. package/src/clientinfo/default.ts +128 -0
  147. package/src/clientinfo/index.browser.ts +4 -0
  148. package/src/clientinfo/index.ts +4 -0
  149. package/src/http/http.ts +75 -0
  150. package/src/http/index.ts +8 -0
  151. package/src/index.ts +5 -0
  152. package/src/logger/index.ts +8 -0
  153. package/src/logger/logger.ts +99 -0
  154. package/src/profiles/errors.ts +28 -0
  155. package/src/profiles/index.browser.ts +10 -0
  156. package/src/profiles/index.ts +15 -0
  157. package/src/profiles/ini.ts +126 -0
  158. package/src/profiles/profile.ts +467 -0
  159. package/src/profiles/resolve.ts +251 -0
  160. package/src/profiles/secret.ts +40 -0
  161. package/src/wkt/fieldmask.ts +89 -0
  162. package/src/wkt/index.ts +3 -0
  163. package/src/wkt/value.ts +19 -0
  164. package/index.js +0 -1
@@ -0,0 +1,253 @@
1
+ import {z} from 'zod';
2
+
3
+ import {Code, codeFromString} from './codes';
4
+ import type {ErrorDetails} from './details';
5
+ import {parseErrorDetails} from './details';
6
+
7
+ // Reusable schema fragment for nullish string fields.
8
+ const nullishString = z
9
+ .string()
10
+ .nullish()
11
+ .transform(v => v ?? '');
12
+
13
+ // Zod schema for parsing the JSON error response body. The schema is lenient
14
+ // to handle the various Databricks API error formats (standard, legacy, SCIM).
15
+ const errorResponseSchema = z.object({
16
+ message: nullishString,
17
+ details: z
18
+ .array(z.unknown())
19
+ .nullish()
20
+ .transform(v => v ?? []),
21
+ // Some Databricks APIs incorrectly return the HTTP status code as an
22
+ // integer rather than the actual error code as a string.
23
+ error_code: z.unknown().optional(),
24
+ // Legacy Databricks APIs (e.g. version 1.2 and earlier) used "error"
25
+ // instead of "message".
26
+ error: nullishString,
27
+ // SCIM error fields (RFC7644 section 3.7.3).
28
+ // The "status" field is intentionally omitted; it duplicates HTTP status.
29
+ detail: nullishString,
30
+ scimType: nullishString,
31
+ });
32
+
33
+ // Constructor options for ApiError.
34
+ interface ApiErrorOptions {
35
+ code: Code;
36
+ message: string;
37
+ details: ErrorDetails;
38
+ httpStatusCode?: number | undefined;
39
+ httpHeader?: Headers | undefined;
40
+ httpBody?: Uint8Array | undefined;
41
+ cause?: unknown;
42
+ }
43
+
44
+ /** ApiError is a transport-agnostic error representing a Databricks API error. */
45
+ export class ApiError extends Error {
46
+ /** The canonical error code of the error. */
47
+ readonly code: Code;
48
+
49
+ /**
50
+ * The structured error details of the error. This is left empty if the
51
+ * error response is not a standard Databricks API error.
52
+ */
53
+ readonly details: ErrorDetails;
54
+
55
+ // The raw HTTP error details, undefined if this is not an HTTP error.
56
+ private readonly httpErr?: {
57
+ readonly statusCode: number;
58
+ readonly header: Headers | undefined;
59
+ readonly body: Uint8Array | undefined;
60
+ };
61
+
62
+ /**
63
+ * Do not use this constructor directly. Use {@link ApiError.fromHttpError}
64
+ * instead. This constructor is only meant for internal and testing use.
65
+ * TODO: Make this constructor private.
66
+ *
67
+ * @private
68
+ */
69
+ constructor(options: ApiErrorOptions) {
70
+ super(options.message, {cause: options.cause});
71
+ this.name = 'ApiError';
72
+ this.code = options.code;
73
+ this.details = options.details;
74
+ if (options.httpStatusCode !== undefined) {
75
+ this.httpErr = {
76
+ statusCode: options.httpStatusCode,
77
+ header: options.httpHeader,
78
+ body: options.httpBody,
79
+ };
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Returns the ApiError's HTTP status code. If the ApiError is not an HTTP
85
+ * error, returns -1.
86
+ */
87
+ get httpStatusCode(): number {
88
+ if (this.httpErr === undefined) {
89
+ return -1;
90
+ }
91
+ return this.httpErr.statusCode;
92
+ }
93
+
94
+ /**
95
+ * Returns the ApiError's HTTP headers. If the ApiError is not an HTTP
96
+ * error, returns undefined.
97
+ */
98
+ get httpHeader(): Headers | undefined {
99
+ if (this.httpErr === undefined) {
100
+ return undefined;
101
+ }
102
+ return this.httpErr.header;
103
+ }
104
+
105
+ /**
106
+ * Returns the ApiError's HTTP body. If the ApiError is not an HTTP error,
107
+ * returns undefined.
108
+ */
109
+ get httpBody(): Uint8Array | undefined {
110
+ if (this.httpErr === undefined) {
111
+ return undefined;
112
+ }
113
+ return this.httpErr.body;
114
+ }
115
+
116
+ /**
117
+ * Parses an HTTP error response into an ApiError. Returns undefined if the
118
+ * status code is 2xx.
119
+ */
120
+ static fromHttpError(
121
+ statusCode: number,
122
+ header: Headers | undefined,
123
+ body: Uint8Array | undefined
124
+ ): ApiError | undefined {
125
+ if (statusCode >= 200 && statusCode < 300) {
126
+ return undefined;
127
+ }
128
+
129
+ const emptyDetails: ErrorDetails = {unknownDetails: []};
130
+
131
+ if (body === undefined || body.length === 0) {
132
+ return new ApiError({
133
+ code: toCode(statusCode),
134
+ message: '',
135
+ details: emptyDetails,
136
+ httpStatusCode: statusCode,
137
+ httpHeader: header,
138
+ httpBody: body,
139
+ });
140
+ }
141
+
142
+ // Decode the body to a string for JSON parsing.
143
+ let parsed: unknown;
144
+ try {
145
+ parsed = JSON.parse(new TextDecoder().decode(body));
146
+ } catch (e: unknown) {
147
+ // The JSON error is simply swallowed, this typically happens when the
148
+ // error does not come directly from a Databricks API. A typical example
149
+ // is when the error is returned by a proxy.
150
+ return new ApiError({
151
+ code: toCode(statusCode),
152
+ message: '',
153
+ details: emptyDetails,
154
+ httpStatusCode: statusCode,
155
+ httpHeader: header,
156
+ httpBody: body,
157
+ cause: e instanceof Error ? e : undefined,
158
+ });
159
+ }
160
+
161
+ const result = errorResponseSchema.safeParse(parsed);
162
+ if (!result.success) {
163
+ return new ApiError({
164
+ code: toCode(statusCode),
165
+ message: '',
166
+ details: emptyDetails,
167
+ httpStatusCode: statusCode,
168
+ httpHeader: header,
169
+ httpBody: body,
170
+ cause: result.error,
171
+ });
172
+ }
173
+
174
+ const errResp = result.data;
175
+
176
+ // Error codes may be missing or be an integer (legacy APIs). In such
177
+ // cases, defer to the HTTP status code to infer the closest canonical
178
+ // error code.
179
+ let errorCode: Code;
180
+ if (typeof errResp.error_code === 'string') {
181
+ errorCode = codeFromString(errResp.error_code);
182
+ } else {
183
+ errorCode = toCode(statusCode);
184
+ }
185
+
186
+ // Determine the error message from available fields.
187
+ let errorMessage = '';
188
+ if (errResp.message !== '') {
189
+ errorMessage = errResp.message;
190
+ } else if (errResp.error !== '') {
191
+ errorMessage = errResp.error;
192
+ } else if (errResp.detail !== '') {
193
+ errorMessage = errResp.detail;
194
+ } else if (errResp.scimType !== '') {
195
+ errorMessage = errResp.scimType;
196
+ }
197
+
198
+ return new ApiError({
199
+ code: errorCode,
200
+ message: errorMessage,
201
+ details: parseErrorDetails(errResp.details),
202
+ httpStatusCode: statusCode,
203
+ httpHeader: header,
204
+ httpBody: body,
205
+ });
206
+ }
207
+ }
208
+
209
+ // Maps an HTTP status code to the closest canonical error code.
210
+ export function toCode(httpCode: number): Code {
211
+ // Canonical mappings.
212
+ switch (httpCode) {
213
+ case 200:
214
+ return Code.OK;
215
+ case 400:
216
+ return Code.INVALID_ARGUMENT;
217
+ case 401:
218
+ return Code.UNAUTHENTICATED;
219
+ case 403:
220
+ return Code.PERMISSION_DENIED;
221
+ case 404:
222
+ return Code.NOT_FOUND;
223
+ case 409:
224
+ return Code.ABORTED;
225
+ case 416:
226
+ return Code.OUT_OF_RANGE;
227
+ case 429:
228
+ return Code.RESOURCE_EXHAUSTED;
229
+ case 501:
230
+ return Code.UNIMPLEMENTED;
231
+ case 503:
232
+ return Code.UNAVAILABLE;
233
+ case 504:
234
+ return Code.DEADLINE_EXCEEDED;
235
+ default:
236
+ break;
237
+ }
238
+
239
+ // Fallback for status codes without a direct canonical mapping.
240
+ if (httpCode >= 200 && httpCode < 300) {
241
+ return Code.OK;
242
+ }
243
+ if (httpCode >= 400 && httpCode < 500) {
244
+ // Most non-canonical 4xx status codes are state related and map
245
+ // to the definition of FailedPrecondition.
246
+ return Code.FAILED_PRECONDITION;
247
+ }
248
+ if (httpCode >= 500 && httpCode < 600) {
249
+ return Code.INTERNAL;
250
+ }
251
+
252
+ return Code.UNKNOWN;
253
+ }
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Defines error codes for API errors and their retry semantics.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ /**
8
+ * Code is a numeric code for an error.
9
+ *
10
+ * The numeric values are stable and can be relied upon across SDK versions.
11
+ */
12
+ enum Code {
13
+ /**
14
+ * Unknown indicates an error that cannot be classified.
15
+ *
16
+ * This code might be used for malformed error responses or error responses
17
+ * using an error code that cannot be mapped to a code in this package.
18
+ */
19
+ UNKNOWN = 0,
20
+
21
+ /** OK indicates the operation completed successfully. */
22
+ OK = 1,
23
+
24
+ /** Canceled indicates the operation was canceled (typically by the caller). */
25
+ CANCELED = 2,
26
+
27
+ /**
28
+ * InvalidArgument indicates the client specified an invalid argument.
29
+ *
30
+ * By contrast with FailedPrecondition, InvalidArgument indicates arguments
31
+ * that are problematic regardless of the state of the system. For example,
32
+ * a malformed request parameter.
33
+ */
34
+ INVALID_ARGUMENT = 3,
35
+
36
+ /**
37
+ * DeadlineExceeded means the operation expired before completion.
38
+ *
39
+ * For operations that modify the state of the system, this error may be
40
+ * returned even if the operation has completed successfully. For
41
+ * example, a successful response from a server could have been delayed
42
+ * long enough for the deadline to expire.
43
+ */
44
+ DEADLINE_EXCEEDED = 4,
45
+
46
+ /**
47
+ * NotFound means a requested entity (e.g. a resource or a file) was
48
+ * not found.
49
+ */
50
+ NOT_FOUND = 5,
51
+
52
+ /**
53
+ * AlreadyExists means an attempt to create an entity failed because one
54
+ * already exists.
55
+ */
56
+ ALREADY_EXISTS = 6,
57
+
58
+ /**
59
+ * PermissionDenied indicates the caller does not have permission to
60
+ * execute the specified operation.
61
+ *
62
+ * This is different from an error returned when the user has exhausted
63
+ * some resource (e.g. too many requests) which is a ResourceExhausted
64
+ * error.
65
+ */
66
+ PERMISSION_DENIED = 7,
67
+
68
+ /**
69
+ * ResourceExhausted indicates some resource has been exhausted, perhaps
70
+ * a per-user quota, or perhaps the entire file system is out of space.
71
+ */
72
+ RESOURCE_EXHAUSTED = 8,
73
+
74
+ /**
75
+ * FailedPrecondition indicates the operation was rejected because the
76
+ * system is not in a state required for the operation's execution.
77
+ * For example, directory to be deleted may be non-empty, an rmdir
78
+ * operation is applied to a non-directory, etc.
79
+ */
80
+ FAILED_PRECONDITION = 9,
81
+
82
+ /**
83
+ * Aborted indicates the operation was aborted, typically due to a
84
+ * concurrency issue like sequencer check failures, transaction aborts,
85
+ * etc.
86
+ */
87
+ ABORTED = 10,
88
+
89
+ /**
90
+ * OutOfRange means the operation was attempted past the valid range.
91
+ * E.g., seeking or reading past end of file.
92
+ *
93
+ * Unlike InvalidArgument, this error indicates a problem that may
94
+ * be fixed if the system state changes. For example, a 32-bit file
95
+ * system will generate InvalidArgument if asked to read at an
96
+ * offset that is not in the range [0,2^32-1], but it will generate
97
+ * OutOfRange if asked to read from an offset past the current
98
+ * file size.
99
+ *
100
+ * There is a fair bit of overlap between FailedPrecondition and
101
+ * OutOfRange. We recommend using OutOfRange (the more specific
102
+ * error) when it applies so that callers who are iterating through
103
+ * a space can easily look for an OutOfRange error to detect when
104
+ * they are done.
105
+ */
106
+ OUT_OF_RANGE = 11,
107
+
108
+ /**
109
+ * Unimplemented indicates the operation is not implemented or not
110
+ * supported/enabled in this service.
111
+ */
112
+ UNIMPLEMENTED = 12,
113
+
114
+ /**
115
+ * Internal indicates an internal error. This means some invariants
116
+ * expected by the underlying system have been broken. If you see
117
+ * this error, something is very broken.
118
+ */
119
+ INTERNAL = 13,
120
+
121
+ /**
122
+ * Unavailable indicates the service is currently unavailable.
123
+ *
124
+ * This is most likely a transient condition and may be corrected by
125
+ * retrying. Though, this might not always be safe to retry if the
126
+ * operation is non-idempotent.
127
+ *
128
+ * The Databricks SDK will generally automatically retry the request
129
+ * with a backoff when encountering this error.
130
+ */
131
+ UNAVAILABLE = 14,
132
+
133
+ /** DataLoss indicates unrecoverable data loss or corruption. */
134
+ DATA_LOSS = 15,
135
+
136
+ /**
137
+ * Unauthenticated indicates the request does not have valid
138
+ * authentication credentials for the operation.
139
+ */
140
+ UNAUTHENTICATED = 16,
141
+ }
142
+
143
+ // Maps Code values to their canonical string representation.
144
+ const CODE_TO_STRING: ReadonlyMap<Code, string> = new Map([
145
+ [Code.UNKNOWN, 'UNKNOWN'],
146
+ [Code.OK, 'OK'],
147
+ [Code.CANCELED, 'CANCELLED'],
148
+ [Code.INVALID_ARGUMENT, 'INVALID_ARGUMENT'],
149
+ [Code.DEADLINE_EXCEEDED, 'DEADLINE_EXCEEDED'],
150
+ [Code.NOT_FOUND, 'NOT_FOUND'],
151
+ [Code.ALREADY_EXISTS, 'ALREADY_EXISTS'],
152
+ [Code.PERMISSION_DENIED, 'PERMISSION_DENIED'],
153
+ [Code.RESOURCE_EXHAUSTED, 'RESOURCE_EXHAUSTED'],
154
+ [Code.FAILED_PRECONDITION, 'FAILED_PRECONDITION'],
155
+ [Code.ABORTED, 'ABORTED'],
156
+ [Code.OUT_OF_RANGE, 'OUT_OF_RANGE'],
157
+ [Code.UNIMPLEMENTED, 'UNIMPLEMENTED'],
158
+ [Code.INTERNAL, 'INTERNAL'],
159
+ [Code.UNAVAILABLE, 'UNAVAILABLE'],
160
+ [Code.DATA_LOSS, 'DATA_LOSS'],
161
+ [Code.UNAUTHENTICATED, 'UNAUTHENTICATED'],
162
+ ]);
163
+
164
+ // Maps canonical strings back to Code values.
165
+ const STRING_TO_CODE: ReadonlyMap<string, Code> = new Map(
166
+ [...CODE_TO_STRING.entries()].map(([code, str]) => [str, code])
167
+ );
168
+
169
+ /**
170
+ * Returns the canonical string representation of an error code.
171
+ *
172
+ * If the code is not recognized, "UNKNOWN" is returned. Note that
173
+ * Code.CANCELED maps to "CANCELLED" (British spelling) to match the gRPC
174
+ * convention.
175
+ */
176
+ function codeToString(code: Code): string {
177
+ return CODE_TO_STRING.get(code) ?? 'UNKNOWN';
178
+ }
179
+
180
+ /**
181
+ * Converts a string representation of an error code to its corresponding
182
+ * Code value. If the string does not match any known code, Code.UNKNOWN is
183
+ * returned.
184
+ */
185
+ function codeFromString(s: string): Code {
186
+ return STRING_TO_CODE.get(s) ?? Code.UNKNOWN;
187
+ }
188
+
189
+ export {Code, codeToString, codeFromString};
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Canonical error codes for Databricks API errors.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ export {Code, codeToString, codeFromString} from './codes';