This repository has been archived by the owner on Jul 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from clementvtrd/feat/json-error
feat: json error handling
- Loading branch information
Showing
11 changed files
with
73 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,3 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
:root { | ||
--foreground-rgb: 0, 0, 0; | ||
--background-start-rgb: 214, 219, 220; | ||
--background-end-rgb: 255, 255, 255; | ||
} | ||
|
||
@media (prefers-color-scheme: dark) { | ||
:root { | ||
--foreground-rgb: 255, 255, 255; | ||
--background-start-rgb: 0, 0, 0; | ||
--background-end-rgb: 0, 0, 0; | ||
} | ||
} | ||
|
||
body { | ||
color: rgb(var(--foreground-rgb)); | ||
background: linear-gradient( | ||
to bottom, | ||
transparent, | ||
rgb(var(--background-end-rgb)) | ||
) | ||
rgb(var(--background-start-rgb)); | ||
} | ||
|
||
@layer utilities { | ||
.text-balance { | ||
text-wrap: balance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use client"; | ||
|
||
export default function MessagesErrorHandler({ | ||
error, | ||
reset | ||
}: { | ||
error: Error & { digest?: string } | ||
reset: () => void | ||
}) { | ||
return ( | ||
<section> | ||
<h1>Messages</h1> | ||
<p> | ||
{error.message} | ||
</p> | ||
<p> | ||
Please try again later. | ||
</p> | ||
<button onClick={reset}>Retry</button> | ||
</section> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import localFont from 'next/font/local'; | ||
|
||
const Luciole = localFont({ | ||
src: [ | ||
{ path: './Luciole-Regular.ttf', weight: '400', style: 'normal'}, | ||
{ path: './Luciole-Bold.ttf', weight: '700', style: 'normal'}, | ||
{ path: './Luciole-Regular-Italic.ttf', weight: '400', style: 'italic'}, | ||
{ path: './Luciole-Bold-Italic.ttf', weight: '700', style: 'italic'}, | ||
] | ||
}); | ||
|
||
export default Luciole; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use server"; | ||
|
||
import { MessageIndexResponse } from '@/app/api/messages/route'; | ||
import { handleJsonResponse } from '@/utils/request'; | ||
|
||
export default async function getAllMessages() { | ||
const response = await fetch( | ||
`${process.env.BASE_URL}/api/messages`, | ||
{ | ||
next: { | ||
tags: ['messages'], | ||
} | ||
} | ||
); | ||
|
||
return handleJsonResponse<MessageIndexResponse>(response, 'An error occurred while fetching messages. Please try again later.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export async function handleJsonResponse<T>( | ||
response: Response, | ||
unknownErrorMessageNotHandleByTheApi: string = 'An unknown error occurred.' | ||
): Promise<T> { | ||
// The original variable name is too long, so I changed it to a shorter one. | ||
const uEMNHBTA = unknownErrorMessageNotHandleByTheApi; | ||
|
||
if (!response.ok) { | ||
const data = await response.json(); | ||
|
||
if ('error' in data) { | ||
throw new Error(data.error); | ||
} | ||
|
||
throw new Error(uEMNHBTA); | ||
} | ||
|
||
return response.json() as Promise<T>; | ||
} |