A full-featured CRM built with React, shadcn/ui, and Supabase.
Improve the error message when user creation fails.
Matthieu Hochlander50d3fd54cc48
diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.ts b/src/components/atomic-crm/providers/supabase/dataProvider.tsindex 45943807..300726ea 100644--- a/src/components/atomic-crm/providers/supabase/dataProvider.ts+++ b/src/components/atomic-crm/providers/supabase/dataProvider.ts@@ -133,10 +133,11 @@ const dataProviderWithCustomMethods = {method: "POST",body,});+ const errorDetails = await error.context.json();if (!data || error) {console.error("salesCreate.error", error);- throw new Error("Failed to create account manager");+ throw new Error(errorDetails.message || "Failed to create the user");}return data;diff --git a/src/components/atomic-crm/sales/SalesCreate.tsx b/src/components/atomic-crm/sales/SalesCreate.tsxindex 61d77294..24585b29 100644--- a/src/components/atomic-crm/sales/SalesCreate.tsx+++ b/src/components/atomic-crm/sales/SalesCreate.tsx@@ -24,8 +24,8 @@ export function SalesCreate() {);redirect("/sales");},- onError: () => {- notify("An error occurred while creating the user.", {+ onError: (error) => {+ notify(error.message || "An error occurred while creating the user.", {type: "error",});},diff --git a/supabase/functions/_shared/utils.ts b/supabase/functions/_shared/utils.tsindex 2195aae1..2ce10837 100644--- a/supabase/functions/_shared/utils.ts+++ b/supabase/functions/_shared/utils.ts@@ -5,8 +5,12 @@ export const corsHeaders = {"Access-Control-Allow-Methods": "POST, PATCH, DELETE",};-export function createErrorResponse(status: number, message: string) {- return new Response(JSON.stringify({ status, message }), {+export function createErrorResponse(+ status: number,+ message: string,+ custom: any = {},+) {+ return new Response(JSON.stringify({ status, message, ...custom }), {headers: { "Content-Type": "application/json", ...corsHeaders },status,});diff --git a/supabase/functions/users/index.ts b/supabase/functions/users/index.tsindex a6c8c345..f824cb8d 100644--- a/supabase/functions/users/index.ts+++ b/supabase/functions/users/index.ts@@ -55,15 +55,17 @@ async function inviteUser(req: Request, currentUserSale: any) {user_metadata: { first_name, last_name },});- const { error: emailError } =- await supabaseAdmin.auth.admin.inviteUserByEmail(email);-if (!data?.user || userError) {console.error(`Error inviting user: user_error=${userError}`);- return createErrorResponse(500, "Internal Server Error");+ return createErrorResponse(userError.status, (userError as Error).message, {+ code: userError.code,+ });}- if (!data?.user || userError || emailError) {+ const { error: emailError } =+ await supabaseAdmin.auth.admin.inviteUserByEmail(email);++ if (!data?.user || emailError) {console.error(`Error inviting user, email_error=${emailError}`);return createErrorResponse(500, "Failed to send invitation mail");}