Skip to content

Commit

Permalink
Merge pull request #1301 from Shelf-nu/improve-error-handling
Browse files Browse the repository at this point in the history
chore: improve error handling and fix a bug on assets index
  • Loading branch information
DonKoko authored Sep 4, 2024
2 parents 74f761a + 2ee419a commit bc108db
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/modules/asset/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import type { ErrorLabel } from "~/utils/error";
import {
ShelfError,
isLikeShelfError,
isNotFoundError,
maybeUniqueConstraintViolation,
} from "~/utils/error";
import { getCurrentSearchParams } from "~/utils/http.server";
Expand Down Expand Up @@ -102,6 +103,7 @@ export async function getAsset<T extends Prisma.AssetInclude | undefined>({
"The asset you are trying to access does not exist or you do not have permission to access it.",
additionalData: { id, organizationId },
label,
shouldBeCaptured: !isNotFoundError(cause),
});
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/modules/booking/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getStatusClasses, isOneDayEvent } from "~/utils/calendar";
import { calcTimeDifference } from "~/utils/date-fns";
import { sendNotification } from "~/utils/emitter/send-notification.server";
import type { ErrorLabel } from "~/utils/error";
import { ShelfError } from "~/utils/error";
import { isNotFoundError, ShelfError } from "~/utils/error";
import { getCurrentSearchParams } from "~/utils/http.server";
import { ALL_SELECTED_KEY } from "~/utils/list";
import { Logger } from "~/utils/logger";
Expand Down Expand Up @@ -870,13 +870,15 @@ export async function getBooking(
},
});
} catch (cause) {
const is404 = isNotFoundError(cause);
throw new ShelfError({
cause,
title: "Booking not found",
message:
"The booking you are trying to access does not exist or you do not have permission to access it.",
additionalData: { booking },
label,
shouldBeCaptured: !is404,
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/routes/_layout+/assets._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {
PermissionEntity,
} from "~/utils/permissions/permission.data";
import { userHasPermission } from "~/utils/permissions/permission.validator.client";
import { validatePermission } from "~/utils/permissions/permission.validator.server";
import { hasPermission } from "~/utils/permissions/permission.validator.server";
import { requirePermission } from "~/utils/roles.server";
import { canImportAssets } from "~/utils/subscription.server";
import { tw } from "~/utils/tw";
Expand Down Expand Up @@ -194,7 +194,7 @@ export async function loader({ context, request }: LoaderFunctionArgs) {
modelName,
canImportAssets:
canImportAssets(tierLimit) &&
(await validatePermission({
(await hasPermission({
organizationId,
userId,
roles: role ? [role] : [],
Expand Down
17 changes: 17 additions & 0 deletions app/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createId } from "@paralleldrive/cuid2";
import { Prisma } from "@prisma/client";
import type { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import type { ValidationError } from "./http";

/**
Expand Down Expand Up @@ -172,6 +173,8 @@ export class ShelfError extends Error {
: shouldBeCaptured) ?? true;
this.status = isLikeShelfError(cause)
? status || cause.status || 500
: isNotFoundError(cause)
? 404
: status || 500;
this.traceId = traceId || createId();
}
Expand All @@ -190,6 +193,20 @@ export function isLikeShelfError(cause: unknown): cause is ShelfError {
);
}

/**
* This helper function is used to check if an error is an instance of `ShelfError` or an object that looks like an `ShelfError`.
*/
export function isNotFoundError(
cause: unknown
): cause is PrismaClientKnownRequestError {
return (
typeof cause === "object" &&
cause !== null &&
"code" in cause &&
cause.code === "P2025"
);
}

export function makeShelfError(
cause: unknown,
additionalData?: AdditionalData
Expand Down
4 changes: 3 additions & 1 deletion app/utils/permissions/permission.validator.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export interface PermissionCheckProps {
entity: PermissionEntity;
}

async function hasPermission(params: PermissionCheckProps): Promise<Boolean> {
export async function hasPermission(
params: PermissionCheckProps
): Promise<Boolean> {
let { userId, entity, action, organizationId, roles } = params;

try {
Expand Down

0 comments on commit bc108db

Please sign in to comment.