Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusLevang committed Aug 1, 2024
1 parent dddb02b commit 42bc719
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/app/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {CatalogTitle} from '@/models/CatalogTitle';
import {Field, Form, Formik} from 'formik';
import {useRouter} from 'next/navigation';
import {Button} from '@nextui-org/button';
import { NotFoundError } from '@/models/Errors';

export default function Page({params}: { params: { id: string } }) {
const router = useRouter();
Expand All @@ -22,8 +23,8 @@ export default function Page({params}: { params: { id: string } }) {
useEffect(() => {
void getLocalTitle(params.id)
.then((data: title) => setLocalTitle(data))
.catch((e: string) => {
if (e.toLowerCase().includes('not found')) {
.catch((e: Error) => {
if (e instanceof NotFoundError) {
setLocalTitle({
id: +params.id,
vendor: '',
Expand Down
5 changes: 3 additions & 2 deletions src/app/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {CatalogTitle} from '@/models/CatalogTitle';
import {getLocalTitle} from '@/services/local.data';
import {title} from '@prisma/client';
import {useRouter} from 'next/navigation';
import { NotFoundError } from '@/models/Errors';

export default function Page({params}: { params: { id: string } }) {
const [catalogTitle, setCatalogTitle] = useState<CatalogTitle>();
Expand All @@ -23,9 +24,9 @@ export default function Page({params}: { params: { id: string } }) {
setLocalTitle(data);
setLocalTitleNotFound(false);
})
.catch((e: string) => {
.catch((e: Error) => {
setLocalTitle(undefined);
if (e.toLowerCase().includes('not found')) {
if (e instanceof NotFoundError) {
setLocalTitleNotFound(true);
} else {
alert('Kunne ikke se etter kontakt- og utgivelsesinformasjon. Kontakt tekst-teamet om problemet vedvarer.');
Expand Down
1 change: 1 addition & 0 deletions src/models/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class NotFoundError extends Error {}
2 changes: 1 addition & 1 deletion src/pages/api/title/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function handlePOST(localTitle: title, res: NextApiResponse) {
update: { ...localTitle },
create: { ...localTitle },
}).catch(e => {
throw new Error(`Failed to create or update title: ${e}`);
return res.status(500).json({error: `Failed to create or update title: ${e}`});
});

return res.status(200).json(localTitle);
Expand Down
5 changes: 3 additions & 2 deletions src/services/local.data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NotFoundError } from '@/models/Errors';
import { title } from '@prisma/client';

export async function getLocalTitle(id: string): Promise<title> {
Expand All @@ -7,9 +8,9 @@ export async function getLocalTitle(id: string): Promise<title> {
case 200:
return await response.json() as Promise<title>;
case 404:
return Promise.reject('Title not found');
return Promise.reject(new NotFoundError('Title not found'));
default:
return Promise.reject('Failed to fetch title');
return Promise.reject(new Error('Failed to fetch title'));
}
}

Expand Down

0 comments on commit 42bc719

Please sign in to comment.