remove sdk/types package and move it to main sdk package

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-02-28 11:09:51 +01:00
parent a898cfb14a
commit 43b2fbe7e4
28 changed files with 48 additions and 393 deletions

View File

@@ -20,7 +20,6 @@
"@mixan/constants": "workspace:^",
"@mixan/db": "workspace:^",
"@mixan/queue": "workspace:^",
"@mixan/types": "workspace:*",
"@mixan/validation": "workspace:^",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-alert-dialog": "^1.0.5",

View File

@@ -1,57 +0,0 @@
import type { NextApiResponse } from 'next';
import type { MixanErrorResponse, MixanIssue } from '@mixan/types';
export class HttpError extends Error {
public status: number;
public message: string;
public issues: MixanIssue[];
constructor(status: number, message: string | Error, issues?: MixanIssue[]) {
super(message instanceof Error ? message.message : message);
this.status = status;
this.message = message instanceof Error ? message.message : message;
this.issues = issues ?? [];
}
toJson(): MixanErrorResponse {
return {
code: this.status,
status: 'error',
message: this.message,
issues: this.issues.length ? this.issues : undefined,
stack: process.env.NODE_ENV !== 'production' ? this.stack : undefined,
};
}
}
export function createIssues(arr: MixanIssue[]) {
throw new HttpError(400, 'Issues', arr);
}
export function createError(status = 500, error: unknown) {
if (error instanceof Error || typeof error === 'string') {
return new HttpError(status, error);
}
return new HttpError(500, 'Unexpected error occured');
}
export function handleError(res: NextApiResponse, error: unknown) {
console.log('-----------------');
console.log('ERROR');
console.log(error);
console.log('-----------------');
if (error instanceof HttpError) {
return res.status(error.status).json(error.toJson());
}
if (error instanceof Error) {
const httpError = createError(500, error);
return res.status(httpError.status).json(httpError.toJson());
}
const httpError = createError(500, error);
return res.status(httpError.status).json(httpError.toJson());
}