Skip to content

Commit

Permalink
chore: throw error on error response (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
lino-levan authored May 16, 2023
1 parent f5c5130 commit 3cc02ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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

0 comments on commit 3cc02ee

Please sign in to comment.