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

@@ -3,6 +3,7 @@ import path from 'path';
import os from 'os';
import ini from 'ini';
import { fileExists } from './fs.js';
import { KnownError } from './error.js';
const parseAssert = (
name: string,
@@ -10,7 +11,7 @@ const parseAssert = (
message: string,
) => {
if (!condition) {
throw new Error(`Invalid config property ${name}: ${message}`);
throw new KnownError(`Invalid config property ${name}: ${message}`);
}
};
@@ -67,7 +68,7 @@ export const setConfigs = async (
for (const [key, value] of keyValues) {
if (!hasOwn(configParsers, key)) {
throw new Error(`Invalid config property: ${key}`);
throw new KnownError(`Invalid config property: ${key}`);
}
const parsed = configParsers[key as ValidKeys](value);

20
src/utils/error.ts Normal file
View File

@@ -0,0 +1,20 @@
import { dim } from 'kolorist';
import { version } from '../../package.json';
export class KnownError extends Error {}
const indent = ' ';
export const handleCliError = (error: any) => {
if (
error instanceof Error
&& !(error instanceof KnownError)
) {
if (error.stack) {
console.error(dim(error.stack.split('\n').slice(1).join('\n')));
}
console.error(`\n${indent}${dim(`aicommits v${version}`)}`);
console.error(`\n${indent}Please open a Bug report with the information above:`);
console.error(`${indent}https://github.com/Nutlope/aicommits/issues/new/choose`);
}
};

View File

@@ -1,10 +1,11 @@
import { execa } from 'execa';
import { KnownError } from './error.js';
export const assertGitRepo = async () => {
const { stdout } = await execa('git', ['rev-parse', '--is-inside-work-tree'], { reject: false });
if (stdout !== 'true') {
throw new Error('The current directory must be a Git repository!');
throw new KnownError('The current directory must be a Git repository!');
}
};

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;