added tooling (eslint, typescript and prettier)
This commit is contained in:
@@ -1,55 +1,52 @@
|
||||
import {
|
||||
type MixanIssue,
|
||||
type MixanErrorResponse
|
||||
} from '@mixan/types'
|
||||
import { type NextApiResponse } from 'next'
|
||||
import { type NextApiResponse } from 'next';
|
||||
|
||||
import { type MixanErrorResponse, type MixanIssue } from '@mixan/types';
|
||||
|
||||
export class HttpError extends Error {
|
||||
public status: number
|
||||
public message: string
|
||||
public issues: MixanIssue[]
|
||||
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 ?? []
|
||||
super(message instanceof Error ? message.message : message);
|
||||
this.status = status;
|
||||
this.message = message instanceof Error ? message.message : message;
|
||||
this.issues = issues ?? [];
|
||||
}
|
||||
|
||||
toJson(): MixanErrorResponse {
|
||||
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: Array<MixanIssue>) {
|
||||
throw new HttpError(400, 'Issues', arr)
|
||||
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)
|
||||
}
|
||||
if (error instanceof Error || typeof error === 'string') {
|
||||
return new HttpError(status, error);
|
||||
}
|
||||
|
||||
return new HttpError(500, 'Unexpected error occured')
|
||||
return new HttpError(500, 'Unexpected error occured');
|
||||
}
|
||||
|
||||
export function handleError(res: NextApiResponse, error: unknown) {
|
||||
if(error instanceof HttpError) {
|
||||
return res.status(error.status).json(error.toJson())
|
||||
}
|
||||
|
||||
if(error instanceof Error) {
|
||||
const httpError = createError(500, error)
|
||||
res.status(httpError.status).json(httpError.toJson())
|
||||
if (error instanceof HttpError) {
|
||||
return res.status(error.status).json(error.toJson());
|
||||
}
|
||||
|
||||
|
||||
const httpError = createError(500, error)
|
||||
res.status(httpError.status).json(httpError.toJson())
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
const httpError = createError(500, error);
|
||||
res.status(httpError.status).json(httpError.toJson());
|
||||
}
|
||||
|
||||
const httpError = createError(500, error);
|
||||
res.status(httpError.status).json(httpError.toJson());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user