From 8d259f1ca484f22b07309e7a139784a579038068 Mon Sep 17 00:00:00 2001 From: jawndiego Date: Tue, 13 Feb 2024 09:48:03 -0500 Subject: [PATCH 1/3] richer error logging --- apps/site/lib/api.ts | 65 ++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/apps/site/lib/api.ts b/apps/site/lib/api.ts index 3b2346b9a..ab6648710 100644 --- a/apps/site/lib/api.ts +++ b/apps/site/lib/api.ts @@ -81,35 +81,48 @@ export async function relayRegisterFor( /* MEDIA SERVICE */ export async function w3sUpload(body: FormData, authToken: string | null) { - const res = await fetch('https://river-media-service.up.railway.app/w3s', { - method: 'POST', - headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined, - body, - }) - if (!res.ok) { - console.error('Could not upload file', await res.text()) - throw new Error('Could not upload file') - } - return res.json() -} - -export async function uploadToMux(body: string, authToken: string | null) { - const res = await fetch( - 'https://river-media-service.up.railway.app/mux/upload', - { + try { + const res = await fetch('https://river-media-service.up.railway.app/w3s', { method: 'POST', headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined, body, - }, - ) - if (!res.ok) { - console.error('Could not process video', await res.text()) - throw new Error('Could not process video') - } - const muxResponseData = await res.json() + }); - return { - id: muxResponseData.id, - playbackId: muxResponseData.playbackId, + if (!res.ok) { + const errorText = await res.text(); + console.error('Could not upload file:', errorText); + throw new Error(`Could not upload file: Server responded with ${res.status} - ${errorText}`) + } + + return res.json() + } catch (error) { + console.error('Upload failed:', error); + throw new Error(`Upload failed: ${error instanceof Error ? error.message : 'Unknown error'}`) } } + +export async function uploadToMux(body: string, authToken: string | null) { + try { + const res = await fetch( + 'https://river-media-service.up.railway.app/mux/upload', + { + method: 'POST', + headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined, + body, + }, + ); + if (!res.ok) { + const errorResponse = await res.json() + console.error('Could not process video:', errorResponse.details) + throw new Error(`Could not process video: ${errorResponse.error} - ${errorResponse.details}`) + } + const muxResponseData = await res.json() + return { + id: muxResponseData.id, + playbackId: muxResponseData.playbackId, + }; + } catch (error) { + console.error('Upload to Mux failed:', error) + throw error + } +} \ No newline at end of file From 1ffe49433be6d5e960f15f16459bb62fea9b4881 Mon Sep 17 00:00:00 2001 From: jawndiego Date: Tue, 13 Feb 2024 11:48:26 -0500 Subject: [PATCH 2/3] 413 and 200 --- apps/site/lib/api.ts | 58 +++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/apps/site/lib/api.ts b/apps/site/lib/api.ts index ab6648710..c26780776 100644 --- a/apps/site/lib/api.ts +++ b/apps/site/lib/api.ts @@ -86,43 +86,41 @@ export async function w3sUpload(body: FormData, authToken: string | null) { method: 'POST', headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined, body, - }); + }) - if (!res.ok) { - const errorText = await res.text(); - console.error('Could not upload file:', errorText); - throw new Error(`Could not upload file: Server responded with ${res.status} - ${errorText}`) + if (res.status === 413) { + console.error('File too large') + throw new Error('File too large. Please try a smaller file.') + } else if (!res.ok) { + const errorText = await res.text() + console.error('Could not upload file', errorText) + throw new Error(`Could not upload file: ${res.status} ${res.statusText} - ${errorText}`) } return res.json() } catch (error) { - console.error('Upload failed:', error); - throw new Error(`Upload failed: ${error instanceof Error ? error.message : 'Unknown error'}`) + console.error('Upload to Mux failed:', error) + throw error } } export async function uploadToMux(body: string, authToken: string | null) { - try { - const res = await fetch( - 'https://river-media-service.up.railway.app/mux/upload', - { - method: 'POST', - headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined, - body, - }, - ); - if (!res.ok) { - const errorResponse = await res.json() - console.error('Could not process video:', errorResponse.details) - throw new Error(`Could not process video: ${errorResponse.error} - ${errorResponse.details}`) - } - const muxResponseData = await res.json() - return { - id: muxResponseData.id, - playbackId: muxResponseData.playbackId, - }; - } catch (error) { - console.error('Upload to Mux failed:', error) - throw error + const res = await fetch( + 'https://river-media-service.up.railway.app/mux/upload', + { + method: 'POST', + headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined, + body, + }, + ) + if (!res.ok) { + console.error('Could not process video', await res.text()) + throw new Error('Could not process video') + } + const muxResponseData = await res.json() + + return { + id: muxResponseData.id, + playbackId: muxResponseData.playbackId, } -} \ No newline at end of file +} From e83cbe0f0cd09e15a4329b89a72db234bdce4860 Mon Sep 17 00:00:00 2001 From: jawndiego Date: Tue, 13 Feb 2024 13:12:40 -0500 Subject: [PATCH 3/3] fix log wording --- apps/site/lib/api.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/site/lib/api.ts b/apps/site/lib/api.ts index c26780776..e9f7d1213 100644 --- a/apps/site/lib/api.ts +++ b/apps/site/lib/api.ts @@ -99,7 +99,7 @@ export async function w3sUpload(body: FormData, authToken: string | null) { return res.json() } catch (error) { - console.error('Upload to Mux failed:', error) + console.error('Upload failed', error) throw error } } @@ -114,8 +114,8 @@ export async function uploadToMux(body: string, authToken: string | null) { }, ) if (!res.ok) { - console.error('Could not process video', await res.text()) - throw new Error('Could not process video') + console.error('Could not upload to Mux', await res.text()) + throw new Error('Could not upload to Mux') } const muxResponseData = await res.json()