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

fix: exception bug for debug mode #56

Merged
merged 1 commit into from
Jan 12, 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
4 changes: 2 additions & 2 deletions src/http/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const onRequest = (config: AxiosRequestConfig): AxiosRequestConfig => {
};

const onRequestError = (error: AxiosError): Promise<AxiosError> => {
Log.debug("[axios] request error", error);
Log.debug("[axios] request error", error.toJSON());
return Promise.reject(error);
};

Expand All @@ -22,7 +22,7 @@ const onResponse = (response: AxiosResponse): AxiosResponse => {
};

const onResponseError = (error: AxiosError): Promise<AxiosError> => {
Log.debug("[axios] response error", error);
Log.debug("[axios] response error", error.toJSON());
return Promise.reject(error);
};

Expand Down
11 changes: 7 additions & 4 deletions src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from "chalk";
import { decycle } from "./stringify";

class Logger {
info(message: any) {
Expand All @@ -10,10 +11,12 @@ class Logger {
return;
}
console.log(
JSON.stringify({
action: action,
data: message,
})
JSON.stringify(
decycle({
action: action,
data: message,
})
)
);
}

Expand Down
45 changes: 45 additions & 0 deletions src/utils/stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const isArray = (value: any) => Array.isArray(value);

const isObject = (value: any) =>
Object.prototype.toString.call(value).slice(8, -1) === "Object";

const validate = (value: any) => {
if (typeof value === "undefined") {
throw new Error("This method requires one parameter");
}

if (!isArray(value) && !isObject(value)) {
throw new TypeError("This method only accepts arrays and objects");
}
};

const findRef = (ref: any, visitedRefs: any) =>
Object.keys(visitedRefs).find((key) => visitedRefs[key] === ref);

export const decycle = (arg: any) => {
validate(arg);

const visitedRefs: any = {};

const recurs = (value: any, path = "$") => {
const ref = findRef(value, visitedRefs);
if (ref) {
return { $ref: ref };
}
if (isArray(value) || isObject(value)) {
visitedRefs[path] = value;

if (isArray(value)) {
return value.map((elem: any, i: any) => recurs(elem, `${path}[${i}]`));
}

return Object.keys(value).reduce((accum: any, key: any) => {
accum[key] = recurs(value[key], `${path}.${key}`);

return accum;
}, {});
}
return value;
};
return recurs(arg);
};
Loading