Skip to content

Commit 257df3c

Browse files
committed
fix: return JSON error responses from upload-attachment API
Error responses were returned as plain text, causing the frontend to fail JSON parsing and display "Unknown error (400)" instead of the actual error message. Also distinguish between empty and invalid wallet address in error messages for better debugging.
1 parent c202602 commit 257df3c

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

app/routes/api.upload-attachment.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,45 @@ async function imageUploadAction({ context, request }: ActionFunctionArgs) {
5959
const verse = formData.get('verse') as string;
6060

6161
if (!file || !path) {
62-
throw new Response('Missing required fields: file, path', { status: 400, statusText: 'Bad Request' });
62+
throw new Response(JSON.stringify({ success: false, error: 'Missing required fields: file, path' }), {
63+
status: 400,
64+
headers: { 'Content-Type': 'application/json' },
65+
});
6366
}
6467

6568
const { accessToken, walletAddress } = context.user as ContextUser;
6669

6770
if (!accessToken) {
68-
throw new Response('Unauthorized (no access token)', { status: 401, statusText: 'Unauthorized' });
71+
throw new Response(JSON.stringify({ success: false, error: 'Unauthorized (no access token)' }), {
72+
status: 401,
73+
headers: { 'Content-Type': 'application/json' },
74+
});
6975
}
7076

7177
const isChatUpload = path === CHAT_UPLOADS_PATH;
7278
const verseId = isChatUpload ? walletAddress : verse;
7379

7480
if (!verseId) {
75-
throw new Response(isChatUpload ? 'User wallet address is not available' : 'Missing required field: verse', {
81+
let error: string;
82+
83+
if (isChatUpload) {
84+
error = walletAddress ? `Invalid wallet address: ${walletAddress}` : 'User wallet address is empty';
85+
} else {
86+
error = 'Missing required field: verse';
87+
}
88+
89+
throw new Response(JSON.stringify({ success: false, error }), {
7690
status: 400,
77-
statusText: 'Bad Request',
91+
headers: { 'Content-Type': 'application/json' },
7892
});
7993
}
8094

8195
const endpoint = env.V8_GAMESERVER_ENDPOINT;
8296

8397
if (!endpoint) {
84-
throw new Response('Game server endpoint is not configured', {
98+
throw new Response(JSON.stringify({ success: false, error: 'Game server endpoint is not configured' }), {
8599
status: 500,
86-
statusText: 'Internal Server Error',
100+
headers: { 'Content-Type': 'application/json' },
87101
});
88102
}
89103

0 commit comments

Comments
 (0)