From 9460ad3b5aef2632ca25c1fbf642b8e364b56dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ti=E1=BA=BFn=20Nguy=E1=BB=85n=20Kh=E1=BA=AFc?= Date: Tue, 11 Jun 2024 23:09:08 +1200 Subject: [PATCH] refactor: add `from` method to custom errors --- packages/core/src/errors.ts | 18 +++++++++++++++--- packages/react/src/hooks/useMutation.ts | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index d0c53beb..b00c5b33 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -1,5 +1,17 @@ -export class ReDotError extends Error {} +export class ReDotError extends Error { + static from(error: T, message?: string): ReDotError { + return new ReDotError(message, { cause: error }); + } +} -export class QueryError extends ReDotError {} +export class QueryError extends ReDotError { + static override from(error: T, message?: string): QueryError { + return new QueryError(message, { cause: error }); + } +} -export class MutationError extends ReDotError {} +export class MutationError extends ReDotError { + static override from(error: T, message?: string): MutationError { + return new MutationError(message, { cause: error }); + } +} diff --git a/packages/react/src/hooks/useMutation.ts b/packages/react/src/hooks/useMutation.ts index f0b56d50..02974375 100644 --- a/packages/react/src/hooks/useMutation.ts +++ b/packages/react/src/hooks/useMutation.ts @@ -87,7 +87,7 @@ export const useMutation = < .subscribe({ next: setState, error: (error) => { - setState(new MutationError(undefined, { cause: error })); + setState(MutationError.from(error)); reject(error); }, complete: resolve,