mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
more improvements on axios and spotify api error handling, logging, other corrections
This commit is contained in:
@@ -25,6 +25,7 @@ axiosInstance.interceptors.request.use(config => {
|
||||
url: config.url,
|
||||
method: config.method,
|
||||
params: config.params ?? {},
|
||||
headers: Object.keys(config.headers),
|
||||
});
|
||||
return config;
|
||||
});
|
||||
@@ -32,9 +33,15 @@ axiosInstance.interceptors.request.use(config => {
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
logger.warn("AxiosError", { req: error.config });
|
||||
if (error.response)
|
||||
return error.response;
|
||||
logger.warn("AxiosError", {
|
||||
error: {
|
||||
name: error.name,
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
},
|
||||
req: error.config,
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -6,27 +6,17 @@ const { colorize, combine, label, timestamp, printf, errors } = format;
|
||||
const typedefs = require("../typedefs");
|
||||
|
||||
const getLabel = (callingModule) => {
|
||||
const parts = callingModule.filename.split(path.sep);
|
||||
if (!callingModule.filename) return "repl";
|
||||
const parts = callingModule.filename?.split(path.sep);
|
||||
return path.join(parts[parts.length - 2], parts.pop());
|
||||
};
|
||||
|
||||
const allowedErrorKeys = ["name", "message", "stack"];
|
||||
|
||||
const logMetaReplacer = (key, value) => {
|
||||
if (key === "error") {
|
||||
return {
|
||||
name: value.name,
|
||||
message: value.message,
|
||||
stack: value.stack,
|
||||
};
|
||||
}
|
||||
return value;
|
||||
}
|
||||
const allowedErrorKeys = ["name", "code", "message", "stack"];
|
||||
|
||||
const metaFormat = (meta) => {
|
||||
if (Object.keys(meta).length > 0)
|
||||
return '\n' + JSON.stringify(meta, logMetaReplacer, "\t") + '\n';
|
||||
return '\n';
|
||||
return '\n' + JSON.stringify(meta, null, "\t");
|
||||
return '';
|
||||
}
|
||||
|
||||
const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
||||
@@ -36,6 +26,9 @@ const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
||||
delete meta.error[key];
|
||||
}
|
||||
}
|
||||
const { stack, ...rest } = meta.error;
|
||||
return `${timestamp} [${label}] ${level}: ${message}${metaFormat(rest)}\n` +
|
||||
`${stack}`;
|
||||
}
|
||||
return `${timestamp} [${label}] ${level}: ${message}${metaFormat(meta)}`;
|
||||
});
|
||||
@@ -46,12 +39,12 @@ const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
||||
* @returns {typedefs.Logger}
|
||||
*/
|
||||
const logger = (callingModule) => {
|
||||
return createLogger({
|
||||
let tmpLogger = createLogger({
|
||||
levels: config.npm.levels,
|
||||
format: combine(
|
||||
errors({ stack: true }),
|
||||
label({ label: getLabel(callingModule) }),
|
||||
timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
|
||||
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||
logFormat,
|
||||
),
|
||||
transports: [
|
||||
@@ -68,6 +61,8 @@ const logger = (callingModule) => {
|
||||
}),
|
||||
]
|
||||
});
|
||||
tmpLogger.on('error', (error) => tmpLogger.crit("Error inside logger", { error }));
|
||||
return tmpLogger;
|
||||
}
|
||||
|
||||
module.exports = logger;
|
||||
Reference in New Issue
Block a user