Skip to content

Commit

Permalink
chore: fix axios error handling, add comments, add logger for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyjackfrost committed Jun 5, 2023
1 parent a65e56c commit 8ed2dfd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
47 changes: 28 additions & 19 deletions packages/extension-driver-canner/src/lib/cannerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from 'axios';
import { PGOptions } from './cannerDataSource';
import { ConnectionOptions } from 'tls';
import { createEnvConfig } from './config';
import { InternalError, getLogger } from '@vulcan-sql/core';

const envConfig = createEnvConfig();

Expand All @@ -11,6 +12,7 @@ export class CannerAdapter {
public readonly PAT: string | (() => string | Promise<string>);
public readonly ssl: boolean | ConnectionOptions;
private baseUrl: string | undefined;
private logger = getLogger({ scopeName: 'CORE' });

constructor(options?: PGOptions) {
if (!options) {
Expand All @@ -30,6 +32,7 @@ export class CannerAdapter {
// and store them in S3. This method will return the S3 urls of the query result.
// For more Canner API ref: https://docs.cannerdata.com/reference/restful
public async createAsyncQueryResultUrls(sql: string): Promise<string[]> {
this.logger.debug(`Create async request to Canner.`);
let data = await this.getWorkspaceRequestData('post', '/v2/async-queries', {
data: {
sql,
Expand All @@ -39,14 +42,18 @@ export class CannerAdapter {
});

const { id: requestId } = data;
this.logger.debug(`Wait Async request to finished.`);
await this.waitAsyncQueryToFinish(requestId);

// get the query result after the query finished
data = await this.getRequestInfo(requestId);
if (data.error?.message) {
throw new Error(data.error.message);
}

this.logger.debug(`Get Query result urls.`);
const urls = await this.getAsyncQueryResultUrls(requestId);
this.logger.debug(`Query result urls: \n${urls.join('\n')}`);
return urls;
}

Expand All @@ -56,25 +63,28 @@ export class CannerAdapter {
options?: Record<string, any>
) {
await this.prepare();
const response = await axios({
headers: {
Authorization: `Token ${this.PAT}`,
},
params: {
workspaceSqlName: this.workspaceSqlName,
},
url: `${this.baseUrl}${urlPath}`,
method,
...options,
});
if (response.status !== 200) {
throw new Error(
`Failed to get workspace request "${urlPath}" data, status: ${
response.status
}, data: ${JSON.stringify(response.data)}`
try {
const response = await axios({
headers: {
Authorization: `Token ${this.PAT}`,
},
params: {
workspaceSqlName: this.workspaceSqlName,
},
url: `${this.baseUrl}${urlPath}`,
method,
...options,
});
return response.data;
} catch (error: any) {
const message = error.response
? `response: ${JSON.stringify(error.response)}`
: `remote server does not response. request ${error.request}`;

throw new InternalError(
`Failed to get workspace request "${urlPath}" data, ${message}`
);
}
return response.data;
}

private async prepare() {
Expand Down Expand Up @@ -118,11 +128,10 @@ export class CannerAdapter {
}

private async getRequestInfo(requestId: string) {
const data = await this.getWorkspaceRequestData(
return await this.getWorkspaceRequestData(
'get',
`/v2/async-queries/${requestId}`
);
return data;
}

private async getAsyncQueryResultUrls(requestId: string): Promise<string[]> {
Expand Down
3 changes: 3 additions & 0 deletions packages/extension-driver-canner/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export interface IEnvConfig {

export const createEnvConfig = (): IEnvConfig => {
return {
// when integrate with the Canner Enterprise, the vulcan server and canner server will be deployed in k8s in the same cluster
// so the protocol and host might be different from the user provided.
// e.g. the user provided host is "my-canner.web.com" with "https", but the actual host is "vulcan-server:3000" with protocol "http"
isOnKubernetes: Boolean(process.env['IS_ON_KUBERNETES']) || false,
webServiceHost: process.env['WEB_SERVICE_HOST'],
} as IEnvConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CannerServer } from './cannerServer';
import { CannerDataSource, PGOptions } from '../src';
import { ExportOptions, streamToArray } from '@vulcan-sql/core';
import { ExportOptions, InternalError, streamToArray } from '@vulcan-sql/core';
import { Writable } from 'stream';
import * as sinon from 'ts-sinon';
import * as fs from 'fs';
Expand Down Expand Up @@ -62,11 +62,14 @@ it('Data source should export successfully', async () => {

it('Data source should throw when fail to export data', async () => {
// Arrange
// stub the private function to manipulate getting error from the remote server
sinon.default
.stub(CannerAdapter.prototype, 'createAsyncQueryResultUrls')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.callsFake(async (sql) => {
throw new Error('mock error');
throw new InternalError(
'Failed to get workspace request "mock/url" data'
);
});

fs.mkdirSync('tmp', { recursive: true });
Expand Down

0 comments on commit 8ed2dfd

Please sign in to comment.