Skip to content

Commit

Permalink
Merge pull request #56 from dieuhd/fix/debug_mode
Browse files Browse the repository at this point in the history
fix: exception bug for debug mode
  • Loading branch information
hblab-dieuhd authored Jan 12, 2024
2 parents ec87a25 + 8fd587f commit 04683ee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
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);
};

0 comments on commit 04683ee

Please sign in to comment.