Skip to content

Commit

Permalink
feat: add more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Dec 10, 2024
1 parent 35ab04a commit cdbd025
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
29 changes: 24 additions & 5 deletions src/lib/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class CustomError extends Error {
acc.push(
val,
...Object.entries(props.at(i) ?? {})
.map(([key, value]) => `"${key}" (${String(value)})`)
.map(([key, value]) => `${key} \`${String(value)}\``)
.join(delimiter),
);
return acc;
Expand Down Expand Up @@ -79,8 +79,8 @@ export class CustomError extends Error {
}

/**
* If the page segment is rendered on server, there is no need for skeleton so it can be thrown indicating it's not meant to be on client.
* */
* Page segment is meant to be rendered on server, but was called on the client.
*/
export class NoSkeletonError extends CustomError {
protected override get defaultDisplay() {
return "Something went wrong while loading the page.";
Expand All @@ -89,14 +89,33 @@ export class NoSkeletonError extends CustomError {

/**
* For functionality left out intentionally, that would only be relevant later.
* */
*/
export class NotImplementedError extends CustomError {}

/**
* Error for custom validations, where `zod` isn't used.
* */
*/
export class ValidationError extends CustomError {
protected override get defaultDisplay() {
return "There are issues with the provided data.";
}
}

/**
* Successful response came in during fetching, but the response could not be
* handled.
*/
export class FetchError extends CustomError {
protected override get defaultDisplay() {
return "Failed to retrieve data.";
}
}

/**
* On parsing a response with zod that isn't supposed to fail. Note that this could also happen when requesting a similar but wrong endpoint.
*/
export class ResponseMismatchError extends CustomError {
protected override get defaultDisplay() {
return "Failed to retrieve data.";
}
}
13 changes: 9 additions & 4 deletions src/lib/fetchGuildApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { signOut } from "@/actions/auth";
import { tryGetToken } from "@/lib/token";
import { Status } from "@reflet/http";
import { env } from "./env";
import { ValidationError } from "./error";
import { FetchError, ValidationError } from "./error";
import type { ErrorLike } from "./types";

type FetchResult<Data, Error> =
Expand Down Expand Up @@ -96,13 +97,15 @@ export const fetchGuildApi = async <Data = object, Error = ErrorLike>(
headers,
});

if (response.status === 401) {
if (response.status === Status.Unauthorized) {
signOut();
}

const contentType = response.headers.get("content-type");
if (!contentType?.includes("application/json")) {
throw new Error("Guild API failed to respond with json");
throw new FetchError({
cause: FetchError.expected`JSON from Guild API response, instead received ${{ contentType }}`,
});
}

logger.info({ response }, "\n", url.toString(), response.status);
Expand All @@ -111,7 +114,9 @@ export const fetchGuildApi = async <Data = object, Error = ErrorLike>(
try {
json = await response.json();
} catch {
throw new Error("Failed to parse json from response");
throw new FetchError({
cause: FetchError.expected`to parse JSON from response`,
});
}

logger.info({ response }, json, "\n");
Expand Down

0 comments on commit cdbd025

Please sign in to comment.