Skip to content

Commit

Permalink
refactor: move to @belgattitude/http-exception
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Sep 14, 2022
1 parent 25a9e85 commit 3576cce
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 167 deletions.
7 changes: 3 additions & 4 deletions apps/nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"symlink-dir": "5.0.1",
"sync-directory": "5.1.7",
"tailwindcss": "3.1.8",
"ts-jest": "29.0.1",
"typescript": "4.8.3",
"vite": "3.1.0",
"vite-plugin-svgr": "2.2.1",
Expand All @@ -112,6 +111,7 @@
},
"dependencies": {
"@babel/core": "7.19.0",
"@belgattitude/http-exception": "1.0.0",
"@emotion/cache": "11.10.3",
"@emotion/react": "11.10.4",
"@emotion/server": "11.10.0",
Expand All @@ -125,7 +125,7 @@
"@sentry/react": "7.12.1",
"@soluble/cache-interop": "0.11.1",
"@soluble/cache-ioredis": "0.12.1",
"@tsed/exceptions": "6.132.0",
"@tanstack/react-query": "4.3.4",
"@your-org/api-gateway": "workspace:^",
"@your-org/common-i18n": "workspace:^",
"@your-org/core-lib": "workspace:^",
Expand All @@ -145,12 +145,11 @@
"next-i18next": "12.0.1",
"next-secure-headers": "2.2.0",
"next-seo": "5.5.0",
"next-transpile-modules": "9.0.0",
"next-transpile-modules": "patch:next-transpile-modules@npm%3A9.0.0#~/.yarn/patches/next-transpile-modules-npm-9.0.0-f224c724ec.patch",
"picocolors": "1.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-i18next": "11.18.6",
"react-query": "4.0.0-beta.23",
"rooks": "7.1.2",
"sharp": "0.31.0",
"superjson": "1.9.1",
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs-app/src/AppProviders.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { EmotionCache } from '@emotion/react';
import { ThemeProvider as MuiThemeProvider } from '@mui/material';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { FC, ReactNode } from 'react';

import { QueryClient, QueryClientProvider } from 'react-query';
import { muiTheme } from '@/themes/mui/mui.theme';

const queryClient = new QueryClient({
Expand Down
17 changes: 13 additions & 4 deletions apps/nextjs-app/src/backend/api/rest/post-repository.ssr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { InternalServerError, NotFound } from '@tsed/exceptions';
import {
HttpInternalServerError,
HttpNotFound,
} from '@belgattitude/http-exception';
import type { UnPromisify } from '@your-org/core-lib';
import { Asserts } from '@your-org/core-lib';
import type { PrismaClientDbMain } from '@your-org/db-main-prisma';
Expand All @@ -21,11 +24,14 @@ export class PostRepositorySsr {
});
Asserts.isPresent(
post,
() => new NotFound(`Post ${postId} can't be found`)
() => new HttpNotFound(`Post ${postId} can't be found`)
);
return post;
} catch (e) {
throw new InternalServerError(`Post ${postId} can't be retrieved`, e);
throw new HttpInternalServerError({
message: `Post ${postId} can't be retrieved`,
cause: e instanceof Error ? e : undefined,
});
}
};

Expand Down Expand Up @@ -55,7 +61,10 @@ export class PostRepositorySsr {
orderBy: { publishedAt: 'desc' },
});
} catch (e) {
throw new InternalServerError(`Posts can't be retrieved`, e);
throw new HttpInternalServerError({
message: `Posts can't be retrieved`,
cause: e instanceof Error ? e : undefined,
});
}
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InternalServerError } from '@tsed/exceptions';
import { HttpInternalServerError } from '@belgattitude/http-exception';
import type { UnPromisify } from '@your-org/core-lib';
import type { PrismaClientDbMain } from '@your-org/db-main-prisma';
import type { SearchPoemsParams } from './SearchPoems.types';
Expand Down Expand Up @@ -43,7 +43,10 @@ export class SearchPoemsQuery {
orderBy: { author: 'desc' },
});
} catch (e) {
throw new InternalServerError(`Poems can't be retrieved`, e);
throw new HttpInternalServerError({
message: `Poems can't be retrieved`,
cause: e instanceof Error ? e : undefined,
});
}
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query';
import type { FC } from 'react';
import { useQuery } from 'react-query';
import { fetchPoemsWithKy } from '../../api/fetch-poems-ky.api';
import { PoemGrid } from '../../components/PoemGrid';

Expand Down
6 changes: 3 additions & 3 deletions apps/nextjs-app/src/pages/api/rest/poem/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MethodNotAllowed } from '@tsed/exceptions';
import { HttpMethodNotAllowed } from '@belgattitude/http-exception';
import { JsonApiResponseFactory } from '@your-org/core-lib/api/json-api';
import { JsonApiErrorFactory } from '@your-org/core-lib/api/json-api/json-api-error.factory';
import type { NextApiRequest, NextApiResponse } from 'next';
Expand Down Expand Up @@ -32,11 +32,11 @@ export default async function handleListPoems(
}
} else {
return res
.status(MethodNotAllowed.STATUS)
.status(HttpMethodNotAllowed.STATUS)
.json(
JsonApiResponseFactory.fromError(
`The HTTP ${req.method} method is not supported at this route.`,
MethodNotAllowed.STATUS
HttpMethodNotAllowed.STATUS
)
);
}
Expand Down
11 changes: 7 additions & 4 deletions apps/nextjs-app/src/pages/api/rest/post/[id].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { BadRequest, MethodNotAllowed } from '@tsed/exceptions';
import {
HttpBadRequest,
HttpMethodNotAllowed,
} from '@belgattitude/http-exception';
import { Asserts } from '@your-org/core-lib';
import { JsonApiResponseFactory } from '@your-org/core-lib/api/json-api';
import { JsonApiErrorFactory } from '@your-org/core-lib/api/json-api/json-api-error.factory';
Expand All @@ -17,7 +20,7 @@ export default async function handleGetPost(
const postRepo = new PostRepositorySsr(prismaClient);

try {
Asserts.safeInteger(postId, () => new BadRequest('Wrong param id'));
Asserts.safeInteger(postId, () => new HttpBadRequest('Wrong param id'));

return res.json(
JsonApiResponseFactory.fromSuccess(await postRepo.getPost(postId))
Expand All @@ -30,11 +33,11 @@ export default async function handleGetPost(
}
} else {
return res
.status(MethodNotAllowed.STATUS)
.status(HttpMethodNotAllowed.STATUS)
.json(
JsonApiResponseFactory.fromError(
`The HTTP ${req.method} method is not supported at this route.`,
MethodNotAllowed.STATUS
HttpMethodNotAllowed.STATUS
)
);
}
Expand Down
6 changes: 3 additions & 3 deletions apps/nextjs-app/src/pages/api/rest/post/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MethodNotAllowed } from '@tsed/exceptions';
import { HttpMethodNotAllowed } from '@belgattitude/http-exception';
import { JsonApiResponseFactory } from '@your-org/core-lib/api/json-api';
import { JsonApiErrorFactory } from '@your-org/core-lib/api/json-api/json-api-error.factory';
import type { NextApiRequest, NextApiResponse } from 'next';
Expand Down Expand Up @@ -27,11 +27,11 @@ export default async function handleListPosts(
}
} else {
return res
.status(MethodNotAllowed.STATUS)
.status(HttpMethodNotAllowed.STATUS)
.json(
JsonApiResponseFactory.fromError(
`The HTTP ${req.method} method is not supported at this route.`,
MethodNotAllowed.STATUS
HttpMethodNotAllowed.STATUS
)
);
}
Expand Down
5 changes: 2 additions & 3 deletions apps/nextjs-app/src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BadRequest } from '@tsed/exceptions';

import { HttpBadRequest } from '@belgattitude/http-exception';
import type { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { homeConfig } from '@/features/home/home.config';
Expand All @@ -20,7 +19,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
) => {
const { locale } = context;
if (locale === undefined) {
throw new BadRequest('locale is missing');
throw new HttpBadRequest('locale is missing');
}
const { i18nNamespaces } = homeConfig;
return {
Expand Down
4 changes: 2 additions & 2 deletions apps/nextjs-app/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequest } from '@tsed/exceptions';
import { HttpBadRequest } from '@belgattitude/http-exception';
import type { GetStaticProps, InferGetStaticPropsType } from 'next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { demoConfig } from '@/features/demo/demo.config';
Expand All @@ -17,7 +17,7 @@ export default function DemoRoute(
export const getStaticProps: GetStaticProps<Props> = async (context) => {
const { locale } = context;
if (locale === undefined) {
throw new BadRequest('locale is missing');
throw new HttpBadRequest('locale is missing');
}
const { i18nNamespaces } = demoConfig;
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"fix-all-files": "eslint . --ext .ts,.tsx,.js,.jsx,.cjs,.mjs --fix"
},
"dependencies": {
"@tsed/exceptions": "^6.100.3",
"@belgattitude/http-exception": "^1.0.0",
"dequal": "^2.0.0"
},
"peerDependencies": {
Expand Down
33 changes: 21 additions & 12 deletions packages/core-lib/src/api/json-api/json-api-error.factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Exception } from '@tsed/exceptions';
import type { HttpException } from '@belgattitude/http-exception';
import { isHttpException } from '@belgattitude/http-exception';
import type { JsonApiError } from './json-api-response.types';

export class JsonApiErrorFactory {
Expand All @@ -10,25 +11,33 @@ export class JsonApiErrorFactory {
typeof error === 'string' || error instanceof Error
? error
: `Unknown error (type of catched variable: ${typeof error}`;
return JsonApiErrorFactory.fromTsedException(e, defaultHttpStatus);
return JsonApiErrorFactory.fromHttpException(e, defaultHttpStatus);
};

static fromTsedException = (
exception: Exception | Error | string,
static fromHttpException = (
exception: HttpException | Error | string,
/** fallback http status if it can't be inferred from exception */
defaultHttpStatus = 500
): JsonApiError => {
let title: string, status: number;
if (typeof exception === 'string') {
title = exception;
status = defaultHttpStatus;
} else {
title = exception.message;
status = 'status' in exception ? exception.status : defaultHttpStatus;
return {
title: exception,
status: defaultHttpStatus,
};
}
if (isHttpException(exception)) {
return {
title: exception.message,
status: exception.statusCode,
};
}
const { message, status, statusCode } = {
...{ status: null, statusCode: null },
...exception,
};
return {
title,
status,
title: message,
status: status ?? statusCode ?? defaultHttpStatus,
};
};
}
Loading

0 comments on commit 3576cce

Please sign in to comment.