Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(errors): stop creating empty error causes (#1010)

Corentin Thomasset927c7d6b314b
diff --git a/.changeset/eight-papayas-exist.md b/.changeset/eight-papayas-exist.md
new file mode 100644
index 00000000..6c2fa9be
--- /dev/null
+++ b/.changeset/eight-papayas-exist.md
@@ -0,0 +1,5 @@
+---
+"@papra/app": patch
+---
+
+Removed logging of a polluting empty error cause (with stack trace) when an error is thrown without a cause.
diff --git a/apps/papra-server/src/modules/shared/errors/errors.test.ts b/apps/papra-server/src/modules/shared/errors/errors.test.ts
index 9159af1a..3178f632 100644
--- a/apps/papra-server/src/modules/shared/errors/errors.test.ts
+++ b/apps/papra-server/src/modules/shared/errors/errors.test.ts
@@ -6,8 +6,13 @@ describe('errors', () => {
test('type guards to check if an error is a custom error', () => {
expect(isCustomError(new Error('foo'))).to.eql(false);
expect(isCustomError({ isCustomError: true })).to.eql(false);
-
expect(isCustomError(createError({ message: 'foo', code: 'bar', statusCode: 500 }))).to.eql(true);
+
+ expect(isCustomError('foo')).to.eql(false);
+ expect(isCustomError(null)).to.eql(false);
+ expect(isCustomError(undefined)).to.eql(false);
+ expect(isCustomError({})).to.eql(false);
+ expect(isCustomError({ message: 'foo', code: 'bar', statusCode: 500 })).to.eql(false);
});
});
@@ -26,8 +31,7 @@ describe('errors', () => {
const cause = new Error('original error');
const error = createError({ message: 'foo', code: 'bar', statusCode: 500, cause });
- expect(error.cause).toBeInstanceOf(Error);
- expect(error.cause?.message).to.eql('original error');
+ expect(error.cause).toBe(cause);
});
});
diff --git a/apps/papra-server/src/modules/shared/errors/errors.ts b/apps/papra-server/src/modules/shared/errors/errors.ts
index 4b626485..fb1bf9ce 100644
--- a/apps/papra-server/src/modules/shared/errors/errors.ts
+++ b/apps/papra-server/src/modules/shared/errors/errors.ts
@@ -1,5 +1,4 @@
import type { ContentfulStatusCode } from 'hono/utils/http-status';
-import { isError } from 'lodash-es';
export { createError, createErrorFactory, formatPublicErrorPayload, isCustomError, isErrorWithCode };
@@ -13,16 +12,15 @@ type ErrorOptions = {
class CustomError extends Error {
code: string;
- cause?: Error | null;
+ cause?: unknown;
statusCode: ContentfulStatusCode;
- isCustomError = true;
isInternal: boolean = false;
constructor({ message, code, cause, statusCode, isInternal = false }: ErrorOptions) {
super(message);
this.code = code;
- this.cause = isError(cause) ? cause : new Error(String(cause));
+ this.cause = cause;
this.statusCode = statusCode;
this.isInternal = isInternal;
}
@@ -43,7 +41,7 @@ function createErrorFactory(baseOption: ErrorOptions) {
}
function isCustomError(error: unknown): error is CustomError {
- return isError(error) && 'isCustomError' in error && error.isCustomError === true;
+ return error instanceof CustomError;
}
function isErrorWithCode({ error, code }: { error: unknown; code: string }) {