Skip to content

Commit

Permalink
refactored the error class
Browse files Browse the repository at this point in the history
  • Loading branch information
vim-diesel committed Nov 26, 2024
1 parent 5d54937 commit 836eb97
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 214 deletions.
11 changes: 11 additions & 0 deletions .excalidraw
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "excalidraw",
"version": 2,
"source": "https://marketplace.visualstudio.com/items?itemName=pomdtr.excalidraw-editor",
"elements": [],
"appState": {
"gridSize": null,
"viewBackgroundColor": "#ffffff"
},
"files": {}
}
61 changes: 37 additions & 24 deletions actions/budget/budgetActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { MonthlyBudget, Budget } from "@/types";
import { createServersideClient } from "@/utils/supabase/server";
import { revalidatePath } from "next/cache";


export async function getDefaultBudget(): Promise<Budget | PlainAppError> {
const supabase = createServersideClient();
const {
Expand All @@ -16,11 +15,13 @@ export async function getDefaultBudget(): Promise<Budget | PlainAppError> {
// Return our custom error type if there is an auth error from Supabase
if (authError || !user?.id) {
console.error("Error authenticating user: ", authError?.message);
return new AppError(
"AUTH_ERROR",
"User authentication failed or user not found",
authError?.code,
).toPlainObject();
return new AppError({
name: "AUTH_ERROR",
message: "User authentication failed or user not found",
code: authError?.code || "AUTH_FAILURE",
status: authError?.status || 401,
hint: { hint: "Try logging in again." },
}).toPlainObject();
}

const { data: budget, error } = await supabase
Expand All @@ -41,42 +42,54 @@ export async function getDefaultBudget(): Promise<Budget | PlainAppError> {
return await createDefaultBudget(budgetName);
}
console.error("Error fetching budgets: ", error);
return new AppError("DB_ERROR", error.message, error.code).toPlainObject();
return new AppError({
name: "DB_ERROR",
message: error.message,
code: error.code,
status: 500,
details: error.details,
}).toPlainObject();
}

// revalidatePath("/dashboard");
return budget;
}

export async function createDefaultBudget(
name: string = "My Budget",
): Promise<Budget | PlainAppError> {
export async function createDefaultBudget(budgetName: string): Promise<Budget | PlainAppError> {
const supabase = createServersideClient();
const {
data: { user },
error: authError,
} = await supabase.auth.getUser();

if (authError || !user?.id) {
console.error("Error authenticating user: ", authError);
return new AppError(
"AUTH_ERROR",
"User authentication failed or user not found",
authError?.code,
authError?.status,
).toPlainObject();
console.error("Error authenticating user: ", authError?.message);
return new AppError({
name: "AUTH_ERROR",
message: "User authentication failed or user not found",
code: authError?.code || "AUTH_FAILURE",
status: authError?.status || 401,
hint: { hint: "Try logging in again." },
}).toPlainObject();
}

const { data, error } = await supabase
const { data: newBudget, error } = await supabase
.from("budgets")
.insert({ name, user_id: user.id })
.select()
.insert([{ name: budgetName, user_id: user.id }])
.single();

if (error) {
console.error("Error creating budget: ", error);
return new AppError("DB_ERROR", error.message, error.code).toPlainObject();
console.error("Error creating default budget: ", error);
return new AppError({
name: "DB_ERROR",
message: error.message,
code: error.code,
status: 500,
details: error.details,
}).toPlainObject();
}

return data;
// Revalidate the path to ensure the new budget is reflected in the UI
revalidatePath("/dashboard");

return newBudget;
}
46 changes: 28 additions & 18 deletions actions/categories/categoriesActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,33 @@ export async function getCategoriesWithDetails(

if (authError || !user) {
console.error("Error authenticating user: ", authError?.message);
return new AppError(
"AUTH_ERROR",
"User authentication failed or user not found",
authError?.code,
authError?.status,
).toPlainObject();
return new AppError({
name: "AUTH_ERROR",
message: "User authentication failed or user not found",
code: authError?.code,
status: authError?.status,
}).toPlainObject();
}

const { data: categoriesWithDetails, error } = await supabase
.from("categories")
.select(`
.select(
`
*,
monthly_category_details (
*
)
`)
`,
)
.eq("monthly_category_details.monthly_budget_id", monthlyBudgetID);

if (error || !categoriesWithDetails) {
console.error("Error fetching catories with details: ", error);
return new AppError("DB_ERROR", error.message, error.code).toPlainObject();
console.error("Error fetching categories with details: ", error);
return new AppError({
name: "DB_ERROR",
message: error.message,
code: error.code,
}).toPlainObject();
}

// Map over the data to flatten `monthly_category_details` to a single object
Expand Down Expand Up @@ -71,12 +78,12 @@ export async function addCategory(

if (authError || !user) {
console.error("Error authenticating user: ", authError?.message);
return new AppError(
"AUTH_ERROR",
"User authentication failed or user not found",
authError?.code,
authError?.status,
).toPlainObject();
return new AppError({
name: "AUTH_ERROR",
message: "User authentication failed or user not found",
code: authError?.code,
status: authError?.status,
}).toPlainObject();
}

const { error } = await supabase
Expand All @@ -85,12 +92,15 @@ export async function addCategory(

if (error) {
console.error("Error adding category: ", error);
return new AppError("DB_ERROR", error.message, error.code).toPlainObject();
return new AppError({
name: "DB_ERROR",
message: error.message,
code: error.code,
}).toPlainObject();
}

revalidatePath("/dashboard");
return null;
}

// Update a category in the categories table

19 changes: 12 additions & 7 deletions actions/categoryGroups/categoryGroupsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export async function getCategoryGroups(

if (authError || !user?.id) {
console.error("Error authenticating user: ", authError?.message);
return new AppError(
"AUTH_ERROR",
"User authentication failed or user not found",
authError?.code,
authError?.status,
).toPlainObject();
return new AppError({
name: "AUTH_ERROR",
message: "User authentication failed or user not found",
code: authError?.code,
status: authError?.status,
}).toPlainObject();
}

const { data, error } = await supabase
Expand All @@ -32,7 +32,12 @@ export async function getCategoryGroups(

if (error || !data) {
console.error("Error fetching category groups: ", error);
return new AppError("DB_ERROR", error.message, error.code).toPlainObject();
return new AppError({
name: "DB_ERROR",
message: error.message,
code: error.code,
status: 500,
}).toPlainObject();
}

return data;
Expand Down
78 changes: 40 additions & 38 deletions actions/helpers/calculateAvailableFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ export async function getAvailableAmount(

if (authError || !user?.id) {
console.error("Error authenticating user: ", authError?.message);
return new AppError(
"AUTH_ERROR",
"User authentication failed or user not found",
authError?.code,
authError?.status,
).toPlainObject();
return new AppError({
name: "AUTH_ERROR",
message: "User authentication failed or user not found",
code: authError?.code,
status: authError?.status,
}).toPlainObject();
}

// Validate currMonth
if (!(month instanceof Date) || isNaN(month.getTime())) {
return new AppError("VALIDATION_ERROR", "Invalid date provided").toPlainObject();
return new AppError({
name: "VALIDATION_ERROR",
message: "Invalid date provided"
}).toPlainObject();
}

// Get all transactions up to the current month
Expand All @@ -57,11 +60,11 @@ export async function getAvailableAmount(

if (transactionsError) {
console.error("Error fetching transactions: ", transactionsError);
return new AppError(
"DB_ERROR",
transactionsError?.message,
transactionsError?.code,
).toPlainObject();
return new AppError({
name: "DB_ERROR",
message: transactionsError?.message,
code: transactionsError?.code,
}).toPlainObject();
} else if (!transactions) {
return null;
}
Expand All @@ -79,11 +82,11 @@ export async function getAvailableAmount(
// TODO: Check if this is true and remove the second if statement.
if (bugdetsError) {
console.error("Error fetching monthly budgets: ", bugdetsError);
return new AppError(
"DB_ERROR",
bugdetsError.message,
bugdetsError.code,
).toPlainObject();
return new AppError({
name: "DB_ERROR",
message: bugdetsError.message,
code: bugdetsError.code,
}).toPlainObject();
} else if (!monthlyBudgets) {
return null;
}
Expand All @@ -102,11 +105,11 @@ export async function getAvailableAmount(
// TODO: Same here.
if (categoryError) {
console.error("Error fetching monthly category details: ", categoryError);
return new AppError(
"DB_ERROR",
categoryError.message,
categoryError.code,
).toPlainObject();
return new AppError({
name: "DB_ERROR",
message: categoryError.message,
code: categoryError.code,
}).toPlainObject();
} else if (!monthlyCategoryDetails) {
return null;
}
Expand Down Expand Up @@ -138,11 +141,6 @@ export async function getAvailableAmount(
return availableAmount;
}


// *************************



// I don't think we need to export this. Just use it in other server actions
// to recalulate the total available amount after a transaction is added.
export async function calculateAvailableAmount(
Expand All @@ -157,11 +155,11 @@ export async function calculateAvailableAmount(

if (authError || !user) {
console.error("Error authenticating user: ", authError?.message);
return new AppError(
"AUTH_ERROR",
"User authentication failed or user not found",
authError?.code,
);
return new AppError({
name: "AUTH_ERROR",
message: "User authentication failed or user not found",
code: authError?.code,
});
}

const { data: transactions, error: transactionError } = await supabase
Expand All @@ -172,11 +170,11 @@ export async function calculateAvailableAmount(

if (transactionError) {
console.error("Error fetching transactions: ", transactionError);
return new AppError(
"DB_ERROR",
transactionError.message,
transactionError.code,
);
return new AppError({
name: "DB_ERROR",
message: transactionError.message,
code: transactionError.code,
});
} else if (!transactions) {
return 0;
}
Expand All @@ -189,7 +187,11 @@ export async function calculateAvailableAmount(

if (categoryError || !monthlyCategoryDetails) {
console.error("Error fetching monthly category details: ", categoryError);
return new AppError("DB_ERROR", categoryError.message, categoryError.code);
return new AppError({
name: "DB_ERROR",
message: categoryError.message,
code: categoryError.code,
});
}

const totalInflow = transactions
Expand Down
Loading

0 comments on commit 836eb97

Please sign in to comment.