feat: implement payment service with error handling and validation
Add new payment service with repository, error handling, and validation Add exceptions package with database and validation utilities Update database package with new schema and zod types Add turbo start script for development
This commit is contained in:
25
packages/exceptions/src/utils/database.ts
Normal file
25
packages/exceptions/src/utils/database.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { ResultAsync, err, ok } from 'neverthrow';
|
||||
import { AppError } from '../types';
|
||||
|
||||
/**
|
||||
* Wraps a database operation in a Result type to handle errors gracefully
|
||||
* @param operation - The database operation to execute
|
||||
* @param errorFactory - Function to create a custom error if the operation fails
|
||||
* @returns Result containing the operation result or error
|
||||
*/
|
||||
export const wrapDbOperation = <T, E extends AppError<unknown>>(
|
||||
operation: () => Promise<T>,
|
||||
errorFactory: (error: unknown) => E
|
||||
): ResultAsync<T, E> => {
|
||||
return ResultAsync.fromPromise(operation(), errorFactory);
|
||||
};
|
||||
|
||||
export const wrapDbOperationWithNull = <T, E extends AppError<unknown>>(
|
||||
operation: () => Promise<T | null>,
|
||||
notFoundError: E,
|
||||
dbErrorFactory: (error: unknown) => E
|
||||
): ResultAsync<T, E> => {
|
||||
return ResultAsync.fromPromise(operation(), (error) =>
|
||||
dbErrorFactory(error)
|
||||
).andThen((result) => (result === null ? err(notFoundError) : ok(result)));
|
||||
};
|
||||
2
packages/exceptions/src/utils/index.ts
Normal file
2
packages/exceptions/src/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './database';
|
||||
export * from './validation';
|
||||
10
packages/exceptions/src/utils/validation.ts
Normal file
10
packages/exceptions/src/utils/validation.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { err, ok, Result } from 'neverthrow';
|
||||
import { output, ZodError, ZodObject } from 'zod';
|
||||
|
||||
export const safeParse =
|
||||
<T extends ZodObject>(schema: T) =>
|
||||
<D>(data: D): Result<output<T>, ZodError<output<T>>> => {
|
||||
const parseResult = schema.safeParse(data);
|
||||
|
||||
return parseResult.success ? ok(parseResult.data) : err(parseResult.error);
|
||||
};
|
||||
Reference in New Issue
Block a user