Skip to content

Commit

Permalink
fix(logging): log entityId in database-service
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Jul 2, 2024
1 parent 70d5b6b commit f7b48f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
28 changes: 20 additions & 8 deletions src/app/core/database/pouch-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class PouchDatabase extends Database {
}
}

throw new DatabaseException(err);
throw new DatabaseException(err, id);
}
}

Expand All @@ -181,7 +181,10 @@ export class PouchDatabase extends Database {
const result = await (await this.getPouchDBOnceReady()).allDocs(options);
return result.rows.map((row) => row.doc);
} catch (err) {
throw new DatabaseException(err);
throw new DatabaseException(
err,
"allDocs; startkey: " + options?.["startkey"],
);
}
}

Expand All @@ -203,7 +206,7 @@ export class PouchDatabase extends Database {
if (err.status === 409) {
return this.resolveConflict(object, forceOverwrite, err);
} else {
throw new DatabaseException(err);
throw new DatabaseException(err, object._id);
}
}
}
Expand Down Expand Up @@ -232,6 +235,11 @@ export class PouchDatabase extends Database {
forceOverwrite,
result,
).catch((e) => {
this.loggingService.warn(
"error during putAll",
e,
objects.map((x) => x._id),
);
return new DatabaseException(e);
});
}
Expand All @@ -253,7 +261,7 @@ export class PouchDatabase extends Database {
return this.getPouchDBOnceReady()
.then((pouchDB) => pouchDB.remove(object))
.catch((err) => {
throw new DatabaseException(err);
throw new DatabaseException(err, object["_id"]);
});
}

Expand Down Expand Up @@ -334,7 +342,10 @@ export class PouchDatabase extends Database {
return this.getPouchDBOnceReady()
.then((pouchDB) => pouchDB.query(fun, options))
.catch((err) => {
throw new DatabaseException(err);
throw new DatabaseException(
err,
typeof fun === "string" ? fun : undefined,
);
});
}

Expand Down Expand Up @@ -421,10 +432,11 @@ export class PouchDatabase extends Database {
* This overwrites PouchDB's error class which only logs limited information
*/
class DatabaseException extends Error {
constructor(error: PouchDB.Core.Error) {
constructor(error: PouchDB.Core.Error, entityId?: string) {
super();
if (error.status === 404) {
error.message = error.message + ` (${error["docId"]})`;

if (entityId) {
error["entityId"] = entityId;
}
Object.assign(this, error);
}
Expand Down
9 changes: 5 additions & 4 deletions src/app/core/logging/logging.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ export class LoggingService {
/**
* Log the message with "warning" level - for unexpected events that the app can still handle gracefully.
* @param message
* @param context
*/
public warn(message: any) {
this.log(message, LogLevel.WARN);
public warn(message: any, ...context: any[]) {
this.log(message, LogLevel.WARN, ...context);
}

/**
* Log the message with "error" level - for unexpected critical events that cannot be handled and will affect functions.
* @param message
* @param context
*/
public error(message: any, context?: any) {
this.log(message, LogLevel.ERROR, context);
public error(message: any, ...context: any[]) {
this.log(message, LogLevel.ERROR, ...context);
}

/**
Expand Down

0 comments on commit f7b48f5

Please sign in to comment.