Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-950] Handling API and network errors #116

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@polymarket/clob-client",
"description": "Typescript client for Polymarket's CLOB",
"version": "4.5.3",
"version": "4.5.4",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
101 changes: 75 additions & 26 deletions src/http-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-depth */
import axios, { AxiosRequestHeaders, Method } from "axios";
import { DropNotificationParams, OrdersScoringParams } from "src/types";
import { isBrowser } from "browser-or-node";
Expand Down Expand Up @@ -35,26 +36,8 @@ export const request = async (
data?: any,
params?: any,
): Promise<any> => {
try {
overloadHeaders(method, headers);
const response = await axios({ method, url: endpoint, headers, data, params });
return response;
} catch (err) {
if (axios.isAxiosError(err)) {
if (err.response) {
console.error("request error", {
status: err.response?.status,
statusText: err.response?.statusText,
data: err.response?.data,
});
return err.response?.data;
} else {
return { error: "connection error" };
}
}

return { error: err };
}
overloadHeaders(method, headers);
return await axios({ method, url: endpoint, headers, data, params });
};

export type QueryParams = Record<string, any>;
Expand All @@ -66,18 +49,84 @@ export interface RequestOptions {
}

export const post = async (endpoint: string, options?: RequestOptions): Promise<any> => {
const resp = await request(endpoint, POST, options?.headers, options?.data, options?.params);
return "error" in resp ? resp : resp.data;
try {
const resp = await request(
endpoint,
POST,
options?.headers,
options?.data,
options?.params,
);
return resp.data;
} catch (err: unknown) {
return errorHandling(err);
}
};

export const get = async (endpoint: string, options?: RequestOptions): Promise<any> => {
const resp = await request(endpoint, GET, options?.headers, options?.data, options?.params);
return "error" in resp ? resp : resp.data;
try {
const resp = await request(endpoint, GET, options?.headers, options?.data, options?.params);
return resp.data;
} catch (err: unknown) {
return errorHandling(err);
}
};

export const del = async (endpoint: string, options?: RequestOptions): Promise<any> => {
const resp = await request(endpoint, DELETE, options?.headers, options?.data, options?.params);
return "error" in resp ? resp : resp.data;
try {
const resp = await request(
endpoint,
DELETE,
options?.headers,
options?.data,
options?.params,
);
return resp.data;
} catch (err: unknown) {
return errorHandling(err);
}
};

const errorHandling = (err: unknown) => {
if (axios.isAxiosError(err)) {
if (err.response) {
console.error(
"[CLOB Client] request error",
JSON.stringify({
status: err.response?.status,
statusText: err.response?.statusText,
data: err.response?.data,
config: err.response?.config,
}),
);
if (err.response?.data) {
if (
typeof err.response?.data === "string" ||
err.response?.data instanceof String
) {
return { error: err.response?.data };
}
if (!Object.prototype.hasOwnProperty.call(err.response?.data, "error")) {
return { error: err.response?.data };
}
// in this case the field 'error' is included
return err.response?.data;
}
}

if (err.message) {
console.error(
"[CLOB Client] request error",
JSON.stringify({
error: err.message,
}),
);
return { error: err.message };
}
}

console.error("[CLOB Client] request error", err);
return { error: err };
};

export const parseOrdersScoringParams = (orderScoringParams?: OrdersScoringParams): QueryParams => {
Expand Down
Loading