Skip to content

Commit

Permalink
throw ollama response error
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceMacD authored Jan 22, 2024
2 parents 6e0bccd + a573625 commit 9f60d79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ResponseError extends Error {
constructor(
public error: string,
public status_code: number,
) {
super(error)
this.name = 'ResponseError'

if (Error.captureStackTrace) {
Error.captureStackTrace(this, ResponseError)
}
}
}
7 changes: 4 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ export const formatAddress = (address: string): string => {
const checkOk = async (response: Response): Promise<void> => {
if (!response.ok) {
let message = `Error ${response.status}: ${response.statusText}`
let errorData: ErrorResponse | null = null

if (response.headers.get('content-type')?.includes('application/json')) {
try {
const errorResponse = (await response.json()) as ErrorResponse
message = errorResponse.error || message
errorData = (await response.json()) as ErrorResponse
message = errorData.error || message
} catch (error) {
console.log('Failed to parse error response as JSON')
}
Expand All @@ -33,7 +34,7 @@ const checkOk = async (response: Response): Promise<void> => {
}
}

throw new Error(message)
throw new ResponseError(message, response.status)
}
}

Expand Down

0 comments on commit 9f60d79

Please sign in to comment.