feat: show error stack on unknown error (#124)

* feat: show error stack on unknown error

* improve message
This commit is contained in:
hiroki osame
2023-03-03 00:58:22 -05:00
committed by GitHub
parent 915a4ada60
commit 5fe127d377
8 changed files with 45 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import https from 'https';
import type { CreateCompletionRequest, CreateCompletionResponse } from 'openai';
import { encoding_for_model as encodingForModel } from '@dqbd/tiktoken';
import { KnownError } from './error.js';
const createCompletion = (
apiKey: string,
@@ -31,7 +32,7 @@ const createCompletion = (
errorMessage += '; Check the API status: https://status.openai.com';
}
return reject(new Error(errorMessage));
return reject(new KnownError(errorMessage));
}
const body: Buffer[] = [];
@@ -46,7 +47,7 @@ const createCompletion = (
request.on('error', reject);
request.on('timeout', () => {
request.destroy();
reject(new Error('Request timed out'));
reject(new KnownError('Request timed out'));
});
request.write(postContent);
@@ -74,7 +75,7 @@ export const generateCommitMessage = async (
* https://platform.openai.com/docs/models/overview#:~:text=to%20Sep%202021-,text%2Ddavinci%2D003,-Can%20do%20any
*/
if (encoder.encode(prompt).length > 4000) {
throw new Error('The diff is too large for the OpenAI API. Try reducing the number of staged changes, or write your own commit message.');
throw new KnownError('The diff is too large for the OpenAI API. Try reducing the number of staged changes, or write your own commit message.');
}
try {
@@ -97,7 +98,7 @@ export const generateCommitMessage = async (
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny.code === 'ENOTFOUND') {
throw new Error(`Error connecting to ${errorAsAny.hostname} (${errorAsAny.syscall}). Are you connected to the internet?`);
throw new KnownError(`Error connecting to ${errorAsAny.hostname} (${errorAsAny.syscall}). Are you connected to the internet?`);
}
throw errorAsAny;