@malloydata/db-publisher 0.0.321 → 0.0.322

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.
@@ -26,6 +26,7 @@ import type {
26
26
  RawAxiosRequestConfig,
27
27
  } from './client';
28
28
  import {Configuration, ConnectionsApi, ConnectionsTestApi} from './client';
29
+ import {AxiosError} from 'axios';
29
30
 
30
31
  interface PublisherConnectionOptions {
31
32
  connectionUri: string;
@@ -147,33 +148,51 @@ export class PublisherConnection
147
148
  _tableKey: string,
148
149
  tablePath: string
149
150
  ): Promise<TableSourceDef> {
150
- const response = await this.connectionsApi.getTable(
151
- this.projectName,
152
- this.name,
153
- tablePath.split('.')[0],
154
- tablePath,
155
- {
156
- headers: PublisherConnection.getAuthHeaders(this.accessToken),
157
- }
158
- );
159
- return JSON.parse(response.data.source as string) as TableSourceDef;
151
+ let result = {} as TableSourceDef;
152
+ try {
153
+ const response = await this.connectionsApi.getTable(
154
+ this.projectName,
155
+ this.name,
156
+ tablePath.split('.')[0],
157
+ tablePath,
158
+ {
159
+ headers: PublisherConnection.getAuthHeaders(this.accessToken),
160
+ }
161
+ );
162
+ result = JSON.parse(response.data.source as string) as TableSourceDef;
163
+ } catch (error: AxiosError | unknown) {
164
+ this.extractAndThrowError(
165
+ error,
166
+ `Failed to fetch table schema for ${tablePath}`
167
+ );
168
+ }
169
+ return result;
160
170
  }
161
171
 
162
172
  public async fetchSelectSchema(
163
173
  sqlRef: SQLSourceRequest
164
174
  ): Promise<SQLSourceDef> {
165
- const request: PostSqlsourceRequest = {
166
- sqlStatement: sqlRef.selectStr,
167
- };
168
- const response = await this.connectionsApi.postSqlsource(
169
- this.projectName,
170
- this.name,
171
- request,
172
- {
173
- headers: PublisherConnection.getAuthHeaders(this.accessToken),
174
- }
175
- );
176
- return JSON.parse(response.data.source as string) as SQLSourceDef;
175
+ let result = {} as SQLSourceDef;
176
+ try {
177
+ const request: PostSqlsourceRequest = {
178
+ sqlStatement: sqlRef.selectStr,
179
+ };
180
+ const response = await this.connectionsApi.postSqlsource(
181
+ this.projectName,
182
+ this.name,
183
+ request,
184
+ {
185
+ headers: PublisherConnection.getAuthHeaders(this.accessToken),
186
+ }
187
+ );
188
+ result = JSON.parse(response.data.source as string) as SQLSourceDef;
189
+ } catch (error: AxiosError | unknown) {
190
+ this.extractAndThrowError(
191
+ error,
192
+ 'Failed to fetch select schema for SQL query'
193
+ );
194
+ }
195
+ return result;
177
196
  }
178
197
 
179
198
  public async estimateQueryCost(_sqlCommand: string): Promise<QueryRunStats> {
@@ -185,76 +204,112 @@ export class PublisherConnection
185
204
  sql: string,
186
205
  options: RunSQLOptions = {}
187
206
  ): Promise<MalloyQueryData> {
188
- // TODO: Add support for abortSignal.
189
- options.abortSignal = undefined;
190
- const request: PostSqlsourceRequest = {
191
- sqlStatement: sql,
192
- };
193
- const response = await this.connectionsApi.postQuerydata(
194
- this.projectName,
195
- this.name,
196
- request,
197
- JSON.stringify(options),
198
- {
199
- headers: PublisherConnection.getAuthHeaders(this.accessToken),
200
- }
201
- );
202
- return JSON.parse(response.data.data as string) as MalloyQueryData;
207
+ let result = {} as MalloyQueryData;
208
+ try {
209
+ // TODO: Add support for abortSignal.
210
+ options.abortSignal = undefined;
211
+ const request: PostSqlsourceRequest = {
212
+ sqlStatement: sql,
213
+ };
214
+ const response = await this.connectionsApi.postQuerydata(
215
+ this.projectName,
216
+ this.name,
217
+ request,
218
+ JSON.stringify(options),
219
+ {
220
+ headers: PublisherConnection.getAuthHeaders(this.accessToken),
221
+ }
222
+ );
223
+ result = JSON.parse(response.data.data as string) as MalloyQueryData;
224
+ } catch (error: AxiosError | unknown) {
225
+ this.extractAndThrowError(error, 'Failed to execute SQL query');
226
+ }
227
+ return result;
203
228
  }
204
229
 
205
230
  public async *runSQLStream(
206
231
  sqlCommand: string,
207
232
  options: RunSQLOptions = {}
208
233
  ): AsyncIterableIterator<QueryDataRow> {
209
- // TODO: Add support for abortSignal.
210
- options.abortSignal = undefined;
211
- // TODO: Add real streaming support to publisher API.
212
- const request: PostSqlsourceRequest = {
213
- sqlStatement: sqlCommand,
214
- };
215
- const response = await this.connectionsApi.postQuerydata(
216
- this.projectName,
217
- this.name,
218
- request,
219
- JSON.stringify(options),
220
- {
221
- headers: PublisherConnection.getAuthHeaders(this.accessToken),
234
+ try {
235
+ // TODO: Add support for abortSignal.
236
+ options.abortSignal = undefined;
237
+ // TODO: Add real streaming support to publisher API.
238
+ const request: PostSqlsourceRequest = {
239
+ sqlStatement: sqlCommand,
240
+ };
241
+ const response = await this.connectionsApi.postQuerydata(
242
+ this.projectName,
243
+ this.name,
244
+ request,
245
+ JSON.stringify(options),
246
+ {
247
+ headers: PublisherConnection.getAuthHeaders(this.accessToken),
248
+ }
249
+ );
250
+ const queryData = JSON.parse(
251
+ response.data.data as string
252
+ ) as MalloyQueryData;
253
+ for (const row of queryData.rows) {
254
+ yield row;
222
255
  }
223
- );
224
- const queryData = JSON.parse(
225
- response.data.data as string
226
- ) as MalloyQueryData;
227
- for (const row of queryData.rows) {
228
- yield row;
256
+ } catch (error: AxiosError | unknown) {
257
+ this.extractAndThrowError(error, 'Failed to execute streaming SQL query');
258
+ return;
229
259
  }
230
260
  }
231
261
 
232
262
  public async test(): Promise<void> {
233
- await this.connectionsTestApi.testConnectionConfiguration(
234
- this.connectionData,
235
- {
236
- headers: PublisherConnection.getAuthHeaders(this.accessToken),
237
- }
238
- );
263
+ try {
264
+ await this.connectionsTestApi.testConnectionConfiguration(
265
+ this.connectionData,
266
+ {
267
+ headers: PublisherConnection.getAuthHeaders(this.accessToken),
268
+ }
269
+ );
270
+ } catch (error: AxiosError | unknown) {
271
+ this.extractAndThrowError(
272
+ error,
273
+ 'Failed to test connection configuration'
274
+ );
275
+ }
239
276
  }
240
277
 
241
278
  public async manifestTemporaryTable(sqlCommand: string): Promise<string> {
242
- const request: PostSqlsourceRequest = {
243
- sqlStatement: sqlCommand,
244
- };
245
- const response = await this.connectionsApi.postTemporarytable(
246
- this.projectName,
247
- this.name,
248
- request,
249
- {
250
- headers: PublisherConnection.getAuthHeaders(this.accessToken),
251
- }
252
- );
253
- return response.data.table as string;
279
+ let result = '';
280
+ try {
281
+ const request: PostSqlsourceRequest = {
282
+ sqlStatement: sqlCommand,
283
+ };
284
+ const response = await this.connectionsApi.postTemporarytable(
285
+ this.projectName,
286
+ this.name,
287
+ request,
288
+ {
289
+ headers: PublisherConnection.getAuthHeaders(this.accessToken),
290
+ }
291
+ );
292
+ result = response.data.table as string;
293
+ } catch (error: AxiosError | unknown) {
294
+ this.extractAndThrowError(error, 'Failed to manifest temporary table');
295
+ }
296
+ return result;
254
297
  }
255
298
 
256
299
  public async close(): Promise<void> {
257
300
  // Can't close the remote connection.
258
301
  return;
259
302
  }
303
+
304
+ private extractAndThrowError(
305
+ error: AxiosError | unknown,
306
+ defaultMessage: string
307
+ ): void {
308
+ if (error instanceof AxiosError) {
309
+ const errorMessage =
310
+ error?.response?.data?.message || error?.message || defaultMessage;
311
+ throw new Error(errorMessage);
312
+ }
313
+ throw error;
314
+ }
260
315
  }
package/tsconfig.json CHANGED
@@ -5,6 +5,8 @@
5
5
  "outDir": "dist",
6
6
  "composite": true
7
7
  },
8
+ "include": ["src/**/*"],
9
+ "exclude": ["scripts", "dist", "node_modules"],
8
10
  "references": [
9
11
  {
10
12
  "path": "../malloy"