Skip to content

Commit

Permalink
feat: specifying socket timeout (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-cybo authored Jul 26, 2023
1 parent ac1fb7c commit b2cdb29
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe("api", () => {
const PROXY_USERNAME = "proxyUser";
const PROXY_PASSWORD = "proxyPass";
const HTTPS_PROXY_WITH_AUTHN = `http://${PROXY_USERNAME}:${PROXY_PASSWORD}@proxy.example.com:3128`;
const DEFAULT_SOCKET_TIMEOUT = 60000;

it("should pass username and password to the apiClient correctly", () => {
const apiClient = buildRestAPIClient({
Expand All @@ -91,6 +92,7 @@ describe("api", () => {
userAgent: expectedUa,
httpsAgent: new https.Agent(),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});

Expand All @@ -114,6 +116,7 @@ describe("api", () => {
userAgent: expectedUa,
httpsAgent: new https.Agent(),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});

Expand All @@ -129,6 +132,7 @@ describe("api", () => {
userAgent: expectedUa,
httpsAgent: new https.Agent(),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});

Expand All @@ -149,6 +153,7 @@ describe("api", () => {
userAgent: expectedUa,
httpsAgent: new https.Agent(),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});

Expand Down Expand Up @@ -176,6 +181,7 @@ describe("api", () => {
userAgent: expectedUa,
httpsAgent: new https.Agent(),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});
it("should pass information of client certificate to the apiClient correctly", () => {
Expand All @@ -199,6 +205,7 @@ describe("api", () => {
passphrase: PFX_FILE_PASSWORD,
}),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});
it("should pass information of proxy server to the apiClient correctly", () => {
Expand All @@ -222,6 +229,7 @@ describe("api", () => {
port: "3128",
}),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});
it("should pass information of client certificate and proxy server to the apiClient correctly", () => {
Expand Down Expand Up @@ -249,6 +257,7 @@ describe("api", () => {
passphrase: PFX_FILE_PASSWORD,
}),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});

Expand Down Expand Up @@ -279,6 +288,7 @@ describe("api", () => {
},
}),
proxy: false,
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
});
});
3 changes: 3 additions & 0 deletions src/kintone/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type RestAPIClientOptions = {
httpsProxy?: string;
};

const DEFAULT_SOCKET_TIMEOUT = 60000;

const buildAuthParam = (options: RestAPIClientOptions) => {
const passwordAuthParam = {
username: options.username,
Expand Down Expand Up @@ -103,5 +105,6 @@ export const buildRestAPIClient = (options: RestAPIClientOptions) => {
pfxFilePath: options.pfxFilePath,
pfxFilePassword: options.pfxFilePassword,
}),
socketTimeout: DEFAULT_SOCKET_TIMEOUT,
});
};
22 changes: 22 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
KintoneRestAPIError,
} from "@kintone/rest-api-client";

type NetworkError = { code?: string; message?: string };

export abstract class CliKintoneError extends Error {
readonly message: string;
readonly detail: string = "";
Expand All @@ -26,7 +28,10 @@ export abstract class CliKintoneError extends Error {
return this._toStringKintoneAllRecordsError(this.cause);
} else if (this.cause instanceof KintoneRestAPIError) {
return this._toStringKintoneRestAPIError(this.cause);
} else if (this._isNetworkError(this.cause)) {
return this._toStringNetworkError(this.cause);
}

return this.cause + "\n";
}

Expand All @@ -49,6 +54,23 @@ export abstract class CliKintoneError extends Error {
}
}

private _isNetworkError(error: unknown): error is NetworkError {
// TODO: once @kintone/rest-api-client officially exports the socket timeout error, we can use it instead.
return (
typeof error === "object" &&
error !== null &&
"code" in error &&
error.code === "ECONNABORTED"
);
}

private _toStringNetworkError(error: NetworkError): string {
let errorMessage = `[${error.code}] ${error.message}\n`;
errorMessage += "The cli-kintone aborted due to a network error.\n";
errorMessage += "Please check your network connection.\n";
return errorMessage;
}

toString(): string {
let errorMessage = "";
if (this.message.length > 0) {
Expand Down

0 comments on commit b2cdb29

Please sign in to comment.