fix(auth): handle API error types correctly in login action
- Introduce ApiError interface for better type safety - Replace generic HttpError handling with specific ApiError casting - Remove unnecessary console log in error handling block - Ensure error details are properly extracted and returned to client
This commit is contained in:
8
src/lib/types/error.ts
Normal file
8
src/lib/types/error.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export interface ApiError {
|
||||||
|
status: number;
|
||||||
|
details: {
|
||||||
|
code: number;
|
||||||
|
msg: string;
|
||||||
|
};
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import type { Actions } from './$types';
|
|||||||
import { fail } from '@sveltejs/kit';
|
import { fail } from '@sveltejs/kit';
|
||||||
import { authService } from '$lib/api/services/authService.ts';
|
import { authService } from '$lib/api/services/authService.ts';
|
||||||
import { HttpError } from '$lib/api/httpClient.ts';
|
import { HttpError } from '$lib/api/httpClient.ts';
|
||||||
|
import type { ApiError } from '$lib/types/error.ts';
|
||||||
|
|
||||||
|
|
||||||
export const actions:Actions = {
|
export const actions:Actions = {
|
||||||
@@ -30,8 +31,8 @@ export const actions:Actions = {
|
|||||||
return {success:true};
|
return {success:true};
|
||||||
}catch ( error){
|
}catch ( error){
|
||||||
if (error instanceof HttpError){
|
if (error instanceof HttpError){
|
||||||
console.log(JSON.stringify(error));
|
const apiError = error as unknown as ApiError;
|
||||||
return fail(400, {message:error.details.msg});
|
return fail(400, {message:apiError.details.msg});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user