Replace type assertion with built-in error detection (#10)
* refactor(api.ts): use built-in axios error detection
This commit is contained in:
+4
-8
@@ -1,5 +1,5 @@
|
|||||||
import { intro, outro } from '@clack/prompts';
|
import { intro, outro } from '@clack/prompts';
|
||||||
import { AxiosError } from 'axios';
|
import axios from 'axios';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import {
|
import {
|
||||||
ChatCompletionRequestMessage,
|
ChatCompletionRequestMessage,
|
||||||
@@ -50,15 +50,11 @@ class OpenAi {
|
|||||||
const message = data.choices[0].message;
|
const message = data.choices[0].message;
|
||||||
|
|
||||||
return message?.content;
|
return message?.content;
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
outro(`${chalk.red('✖')} ${error}`);
|
outro(`${chalk.red('✖')} ${error}`);
|
||||||
|
|
||||||
if (error.isAxiosError && error.response?.status === 401) {
|
if (axios.isAxiosError<{ error?: { message: string } }>(error) && error.response?.status === 401) {
|
||||||
const err = error as AxiosError;
|
const openAiError = error.response.data.error;
|
||||||
|
|
||||||
const openAiError = (
|
|
||||||
err.response?.data as { error?: { message: string } }
|
|
||||||
).error;
|
|
||||||
|
|
||||||
if (openAiError?.message) outro(openAiError.message);
|
if (openAiError?.message) outro(openAiError.message);
|
||||||
outro(
|
outro(
|
||||||
|
|||||||
Reference in New Issue
Block a user