Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: throw error on error response #12

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Unofficial Deno wrapper for the Open Ai API
# Unofficial Deno wrapper for the Open AI API

[![Tags](https://img.shields.io/github/release/load1n9/openai)](https://github.com/load1n9/openai/releases)
[![Doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/openai/mod.ts)
Expand All @@ -7,7 +7,9 @@

## Usage

Your Open AI Api key ([found here](https://beta.openai.com/account/api-keys)) is needed for this library to work. We recommend setting it as an environment variable. Here is a configuration example.
Your Open AI Api key ([found here](https://beta.openai.com/account/api-keys)) is
needed for this library to work. We recommend setting it as an environment
variable. Here is a configuration example.

```ts
import { OpenAI } from "https://deno.land/x/openai/mod.ts";
Expand Down
17 changes: 15 additions & 2 deletions src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type {
Translation,
TranslationOptions,
} from "./types.ts";
import { basename } from "https://deno.land/std@0.185.0/path/mod.ts";
import { basename } from "https://deno.land/std@0.187.0/path/mod.ts";

const defaultBaseUrl = "https://api.openai.com/v1";

Expand Down Expand Up @@ -65,8 +65,21 @@ export class OpenAI {
method: options?.method ?? "POST",
},
);
const data = await response.json();

if (data.error) {
let errorMessage = `${data.error.type}`;
if (data.error.message) {
errorMessage += ": " + data.error.message;
}
if (data.error.code) {
errorMessage += ` (${data.error.code})`;
}
console.log(data.error);
throw new Error(errorMessage);
}

return await response.json();
return data;
}

/**
Expand Down