Skip to content

Commit

Permalink
fix: return a more precise error code
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Mar 28, 2024
1 parent 35cd06b commit ef5cde2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand All @@ -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);
}
});

Expand Down
10 changes: 4 additions & 6 deletions src/lib/ai/summary.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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;
}
};
}
Expand Down
6 changes: 2 additions & 4 deletions src/lib/ai/textToSpeech.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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;
}
};
}

0 comments on commit ef5cde2

Please sign in to comment.