Skip to content

Commit

Permalink
Log errors as JSON (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso authored Nov 20, 2024
1 parent 51fba77 commit d4c2d24
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function main() {
config,
);

log.info(`Processsed args: ${JSON.stringify(processedArgs, null, 4)}`);
log.info({ cliArgs: processedArgs });
if (processedArgs.api) {
if (process.env.NODE_ENV === "development") {
await new ReloadingAPI(envPath, config, pair, authAccount, processedArgs)
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/tests/bundle/bundle.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ describe("provider bundle", () => {
const { stdout: runOut, stderr: runErr } = await execPromise(
`cd ${rootDir} && node dist/bundle/provider.cli.bundle.js version`,
);
assert(runErr.includes("Version:"));
assert(runOut.includes("Version:"));
}, 120000);
});
14 changes: 4 additions & 10 deletions packages/common/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,12 @@ export abstract class ProsopoBaseError<
this.translationKey = error;
this.context = options?.context;
}
if (!options?.silent) this.logError(logger, logLevel);
if (!options?.silent) this.logError(logger, logLevel, options?.name);
}

private logError(logger: Logger, logLevel: LogLevel) {
const errorFormatter = "\n*************** ERROR ***************\n";
const errorName = `Error Type: ${this.name}\n`;
const errorParams = JSON.stringify(
{ error: this.message, context: this.context },
null,
4,
);
const errorMessage = `${errorFormatter}${errorName}${errorParams}`;
private logError(logger: Logger, logLevel: LogLevel, errorName?: string) {
const errorParams = { error: this.message, context: this.context };
const errorMessage = { errorType: errorName || this.name, errorParams };
logger[logLevel](errorMessage);
}
}
Expand Down
16 changes: 13 additions & 3 deletions packages/common/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import consola, {
LogLevels as ConsolaLogLevels,
createConsola,
type ConsolaOptions,
type LogObject,
} from "consola/browser";
import { enum as zEnum, type infer as zInfer } from "zod";
import { ProsopoEnvError } from "./error.js";
Expand Down Expand Up @@ -50,9 +52,17 @@ export function getLoggerDefault(): Logger {
return defaultLogger;
}

// biome-ignore lint/suspicious/noExplicitAny: we should be able to log anything we want, plus we can't control what external libraries log
const JSONReporter = (message: any) => {
process.stderr.write(`${JSON.stringify(message)}\n`);
const JSONReporter = (
message: LogObject,
context: {
options: ConsolaOptions;
},
) => {
if (context.options.level === ConsolaLogLevels.error) {
process.stderr.write(`${JSON.stringify(message)}\n`);
} else {
process.stdout.write(`${JSON.stringify(message)}\n`);
}
};

const getLoggerAdapterConsola = (logLevel: LogLevel, scope: string): Logger => {
Expand Down
6 changes: 5 additions & 1 deletion packages/provider/src/api/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export const verifySignature = (

if (!pair.verify(timestamp, u8Sig, pair.publicKey)) {
throw new ProsopoApiError("GENERAL.INVALID_SIGNATURE", {
context: { error: "Signature verification failed", code: 401 },
context: {
error: "Signature verification failed",
code: 401,
account: pair.publicKey,
},
});
}
};
17 changes: 14 additions & 3 deletions packages/provider/src/api/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,20 @@ export function prosopoRouter(env: ProviderEnvironment): Router {
if (!sessionRecord) {
return next(
new ProsopoApiError("API.BAD_REQUEST", {
context: { error: "Session ID not found", code: 400 },
context: {
error: "Session ID not found",
code: 400,
siteKey: dapp,
user,
},
}),
);
}
} else if (!(clientSettings?.settings?.captchaType === "pow")) {
// Throw an error
return next(
new ProsopoApiError("API.INCORRECT_CAPTCHA_TYPE", {
context: { code: 400, siteKey: dapp },
context: { code: 400, siteKey: dapp, user },
}),
);
}
Expand All @@ -285,7 +290,12 @@ export function prosopoRouter(env: ProviderEnvironment): Router {
if (!origin) {
return next(
new ProsopoApiError("API.BAD_REQUEST", {
context: { error: "Origin header not found", code: 400 },
context: {
error: "Origin header not found",
code: 400,
siteKey: dapp,
user,
},
}),
);
}
Expand Down Expand Up @@ -330,6 +340,7 @@ export function prosopoRouter(env: ProviderEnvironment): Router {
context: {
code: 500,
siteKey: req.body.dapp,
user: req.body.user,
},
}),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/provider/src/api/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function prosopoVerifyRouter(env: ProviderEnvironment): Router {
if (!clientRecord) {
return next(
new ProsopoApiError("API.SITE_KEY_NOT_REGISTERED", {
context: { code: 400, siteKey: dapp },
context: { code: 400, siteKey: dapp, user },
}),
);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ export function prosopoVerifyRouter(env: ProviderEnvironment): Router {
tasks.logger.error({ err, body: req.body });
return next(
new ProsopoApiError("API.BAD_REQUEST", {
context: { code: 500 },
context: { code: 500, siteKey: req.body.dapp, user: req.body.user },
}),
);
}
Expand Down

0 comments on commit d4c2d24

Please sign in to comment.