From ef5cde26edcfa6d7eb6d1e214257820959ed2003 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Thu, 28 Mar 2024 16:42:06 +0400 Subject: [PATCH] fix: return a more precise error code --- src/api.ts | 8 ++++---- src/lib/ai/summary.ts | 10 ++++------ src/lib/ai/textToSpeech.ts | 6 ++---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/api.ts b/src/api.ts index ed6a531..426e55d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -55,9 +55,9 @@ router.post('/ai/summary/:id', async (req, res) => { } return rpcSuccess(res.status(200), summary, id); - } catch (e) { + } catch (e: any) { capture(e); - return rpcError(res, 'INTERNAL_ERROR', id); + return rpcError(res, e.message || 'INTERNAL_ERROR', id); } }); @@ -84,9 +84,9 @@ router.post('/ai/tts/:id', async (req, res) => { res.header('Content-Type', 'audio/mpeg'); res.attachment(aiTextTpSpeech.filename); return res.end(audio); - } catch (e) { + } catch (e: any) { capture(e); - return rpcError(res, 'INTERNAL_ERROR', id); + return rpcError(res, e.message || 'INTERNAL_ERROR', id); } }); diff --git a/src/lib/ai/summary.ts b/src/lib/ai/summary.ts index d228494..92853ec 100644 --- a/src/lib/ai/summary.ts +++ b/src/lib/ai/summary.ts @@ -1,5 +1,4 @@ import OpenAI from 'openai'; -import { capture } from '@snapshot-labs/snapshot-sentry'; import { fetchProposal, Proposal } from '../../helpers/snapshot'; import { IStorage } from '../storage/types'; import Cache from '../cache'; @@ -18,7 +17,7 @@ class Summary extends Cache { this.proposal = await fetchProposal(this.id); if (!this.proposal) { - return Promise.reject('RECORD_NOT_FOUND'); + throw new Error('RECORD_NOT_FOUND'); } return true; @@ -41,18 +40,17 @@ class Summary extends Cache { }); if (completion.choices.length === 0) { - throw new Error('No completion in response'); + throw new Error('EMPTY_OPENAI_CHOICES'); } const content = completion.choices[0].message.content; if (!content) { - throw new Error('No content in response'); + throw new Error('EMPTY_OPENAI_RESPONSE'); } return content; } catch (e: any) { - capture(e); - throw e; + throw e.error?.code ? new Error(e.error?.code.toUpperCase()) : e; } }; } diff --git a/src/lib/ai/textToSpeech.ts b/src/lib/ai/textToSpeech.ts index 3317978..d352625 100644 --- a/src/lib/ai/textToSpeech.ts +++ b/src/lib/ai/textToSpeech.ts @@ -1,5 +1,4 @@ import OpenAI from 'openai'; -import { capture } from '@snapshot-labs/snapshot-sentry'; import removeMd from 'remove-markdown'; import Cache from '../cache'; import { fetchProposal, Proposal } from '../../helpers/snapshot'; @@ -22,7 +21,7 @@ export default class TextToSpeech extends Cache { this.proposal = await fetchProposal(this.id); if (!this.proposal) { - return Promise.reject('RECORD_NOT_FOUND'); + throw new Error('RECORD_NOT_FOUND'); } return true; @@ -45,8 +44,7 @@ export default class TextToSpeech extends Cache { return Buffer.from(await mp3.arrayBuffer()); } catch (e: any) { - capture(e); - throw e; + throw e.error?.code ? new Error(e.error?.code.toUpperCase()) : e; } }; }