refacto: errors in apis
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { PreferencesService } from "@/lib/services/preferences.service";
|
||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||
import { ERROR_MESSAGES } from "@/constants/errorMessages";
|
||||
import { AppError } from "@/utils/errors";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
@@ -7,7 +10,26 @@ export async function GET() {
|
||||
return NextResponse.json(preferences);
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération des préférences:", error);
|
||||
return new NextResponse("Erreur lors de la récupération des préférences", { status: 500 });
|
||||
if (error instanceof AppError) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: error.code,
|
||||
message: ERROR_MESSAGES[error.code],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: ERROR_CODES.PREFERENCES.FETCH_ERROR,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.PREFERENCES.FETCH_ERROR],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +40,25 @@ export async function PUT(request: NextRequest) {
|
||||
return NextResponse.json(updatedPreferences);
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la mise à jour des préférences:", error);
|
||||
return new NextResponse("Erreur lors de la mise à jour des préférences", { status: 500 });
|
||||
if (error instanceof AppError) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: error.code,
|
||||
message: ERROR_MESSAGES[error.code],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: ERROR_CODES.PREFERENCES.UPDATE_ERROR,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.PREFERENCES.UPDATE_ERROR],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user