1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-01 21:24:45 +02:00

Server: Display more debug info in error log

This commit is contained in:
Laurent Cozic 2021-11-16 10:50:18 +00:00
parent ed31778e2b
commit 3716972829
4 changed files with 44 additions and 18 deletions

View File

@ -33,7 +33,18 @@ export default async function(ctx: AppContext) {
}
} catch (error) {
if (error.httpCode >= 400 && error.httpCode < 500) {
ctx.joplin.appLogger().error(`${error.httpCode}: ` + `${ctx.request.method} ${ctx.path}` + `: ${userIp(ctx)}: ${error.message}`);
const owner = ctx.joplin.owner;
const line: string[] = [
error.httpCode,
`${ctx.request.method} ${ctx.path}`,
owner ? owner.id : userIp(ctx),
error.message,
];
if (error.details) line.push(JSON.stringify(error.details));
ctx.joplin.appLogger().error(line.join(': '));
} else {
ctx.joplin.appLogger().error(userIp(ctx), error);
}

View File

@ -28,7 +28,7 @@ export default class SessionModel extends BaseModel<Session> {
public async authenticate(email: string, password: string): Promise<Session> {
const user = await this.models().user().login(email, password);
if (!user) throw new ErrorForbidden('Invalid username or password');
if (!user) throw new ErrorForbidden('Invalid username or password', { details: { email } });
return this.createUserSession(user.id);
}

View File

@ -16,7 +16,7 @@ router.post('api/sessions', async (_path: SubPath, ctx: AppContext) => {
const fields: User = await bodyFields(ctx.req);
const user = await ctx.joplin.models.user().login(fields.email, fields.password);
if (!user) throw new ErrorForbidden('Invalid username or password');
if (!user) throw new ErrorForbidden('Invalid username or password', { details: { email: fields.email } });
const session = await ctx.joplin.models.session().createUserSession(user.id);
return { id: session.id, user_id: session.user_id };

View File

@ -1,15 +1,30 @@
export interface ErrorOptions {
details?: any;
code?: string;
}
// For explanation of the setPrototypeOf call, see:
// https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
export class ApiError extends Error {
public static httpCode: number = 400;
public httpCode: number;
public code: string;
public constructor(message: string, httpCode: number = null, code: string = undefined) {
public details: any;
public constructor(message: string, httpCode: number = null, code: string | ErrorOptions = undefined) {
super(message);
this.httpCode = httpCode === null ? 400 : httpCode;
this.code = code;
if (typeof code === 'string') {
this.code = code;
} else {
const options: ErrorOptions = { ...code };
this.code = options.code;
this.details = options.details;
}
Object.setPrototypeOf(this, ApiError.prototype);
}
}
@ -23,8 +38,8 @@ export class ErrorWithCode extends ApiError {
export class ErrorMethodNotAllowed extends ApiError {
public static httpCode: number = 400;
public constructor(message: string = 'Method Not Allowed') {
super(message, ErrorMethodNotAllowed.httpCode);
public constructor(message: string = 'Method Not Allowed', options: ErrorOptions = null) {
super(message, ErrorMethodNotAllowed.httpCode, options);
Object.setPrototypeOf(this, ErrorMethodNotAllowed.prototype);
}
}
@ -41,8 +56,8 @@ export class ErrorNotFound extends ApiError {
export class ErrorForbidden extends ApiError {
public static httpCode: number = 403;
public constructor(message: string = 'Forbidden') {
super(message, ErrorForbidden.httpCode);
public constructor(message: string = 'Forbidden', options: ErrorOptions = null) {
super(message, ErrorForbidden.httpCode, options);
Object.setPrototypeOf(this, ErrorForbidden.prototype);
}
}
@ -50,8 +65,8 @@ export class ErrorForbidden extends ApiError {
export class ErrorBadRequest extends ApiError {
public static httpCode: number = 400;
public constructor(message: string = 'Bad Request') {
super(message, ErrorBadRequest.httpCode);
public constructor(message: string = 'Bad Request', options: ErrorOptions = null) {
super(message, ErrorBadRequest.httpCode, options);
Object.setPrototypeOf(this, ErrorBadRequest.prototype);
}
@ -60,8 +75,8 @@ export class ErrorBadRequest extends ApiError {
export class ErrorPreconditionFailed extends ApiError {
public static httpCode: number = 412;
public constructor(message: string = 'Precondition Failed') {
super(message, ErrorPreconditionFailed.httpCode);
public constructor(message: string = 'Precondition Failed', options: ErrorOptions = null) {
super(message, ErrorPreconditionFailed.httpCode, options);
Object.setPrototypeOf(this, ErrorPreconditionFailed.prototype);
}
@ -70,8 +85,8 @@ export class ErrorPreconditionFailed extends ApiError {
export class ErrorUnprocessableEntity extends ApiError {
public static httpCode: number = 422;
public constructor(message: string = 'Unprocessable Entity') {
super(message, ErrorUnprocessableEntity.httpCode);
public constructor(message: string = 'Unprocessable Entity', options: ErrorOptions = null) {
super(message, ErrorUnprocessableEntity.httpCode, options);
Object.setPrototypeOf(this, ErrorUnprocessableEntity.prototype);
}
}
@ -97,8 +112,8 @@ export class ErrorResyncRequired extends ApiError {
export class ErrorPayloadTooLarge extends ApiError {
public static httpCode: number = 413;
public constructor(message: string = 'Payload Too Large') {
super(message, ErrorPayloadTooLarge.httpCode);
public constructor(message: string = 'Payload Too Large', options: ErrorOptions = null) {
super(message, ErrorPayloadTooLarge.httpCode, options);
Object.setPrototypeOf(this, ErrorPayloadTooLarge.prototype);
}
}