Skip to content

Commit

Permalink
feat(presto-client): return back the whole error object (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
alonsovb authored Jan 18, 2024
1 parent ec17cae commit 8221bdc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ npm-debug.log
.DS_Store
Thumbs.db

# Nx files
.nx
9 changes: 9 additions & 0 deletions apps/nest-server/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ export class AppController {
console.error(err)
}
}

@Get('presto-error')
async getDataWithError() {
try {
return await this.appService.getDataWithError()
} catch (err) {
console.error(err)
}
}
}
34 changes: 30 additions & 4 deletions apps/nest-server/src/app/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'
import PrestoClient, { PrestoClientConfig } from '@prestodb/presto-js-client'
import PrestoClient, { PrestoClientConfig, PrestoError } from '@prestodb/presto-js-client'

@Injectable()
export class AppService {
Expand All @@ -18,9 +18,35 @@ export class AppService {
)
return { columns: results.columns, rows: results.data }
} catch (error) {
return JSON.stringify({
error,
})
return (error as PrestoError).message
}
}

async getDataWithError(): Promise<unknown> {
const clientParams: PrestoClientConfig = {
catalog: 'tpch',
host: 'http://localhost',
port: 8080,
schema: 'sf1',
user: 'root',
}
const client = new PrestoClient(clientParams)
try {
const results = await client.query(`SELECT * FROM A SYNTAX ERROR`)
return { columns: results.columns, rows: results.data }
} catch (error) {
if (error instanceof PrestoError) {
/* eslint-disable no-console */
// The error here contains all the information returned by Presto directly
console.info(error.message)
console.info(error.name)
console.info(error.errorCode)
console.info(error.stack)
console.info(error.failureInfo.type)
return 'A Presto error ocurred, please check the service logs'
}
console.error(error)
return error
}
}
}
21 changes: 9 additions & 12 deletions presto-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ If the query succeeds, the PrestoQuery object will have the following properties
- `data`: An array of arrays that contain the actual data for the results.
- `queryId`: The ID of the query.

If the query fails, the PrestoQuery object will have the following properties:

- `error`: An object that contains information about the error.
- `queryId`: The ID of the query.

You can use the `error` property to get more information about the error that occurred. You can also use the `queryId` property to cancel the query or to get more information about the status of the query.
If the query fails, you can catch the error as a PrestoError which contains all information returned by Presto.

### Example usage

Expand All @@ -86,12 +81,14 @@ const client = new PrestoClient({

const query = `SELECT * FROM my_table`

const prestoQuery = await client.query(query)

if (prestoQuery.error) {
// Handle the error.
} else {
// Use the results of the query.
try {
const prestoQuery = await client.query(query)
const results = prestoQuery.data
} catch (error) {
if (error instanceof PrestoError) {
// Handle the error.
console.error(error.errorCode)
}
}
```

Expand Down
8 changes: 6 additions & 2 deletions presto-client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrestoClientConfig, PrestoQuery, PrestoResponse } from './client.types'
import { PrestoClientConfig, PrestoError, PrestoQuery, PrestoResponse } from './client.types'

export class PrestoClient {
private baseUrl: string
Expand Down Expand Up @@ -54,6 +54,9 @@ export class PrestoClient {
})
}

/**
* @throws {PrestoError} If the underlying Presto engine returns an error
*/
async query(
query: string,
options?: {
Expand Down Expand Up @@ -110,7 +113,8 @@ export class PrestoClient {
}

if (prestoResponse.error) {
throw new Error(prestoResponse.error.errorName)
// Throw back the whole error object which contains all error information
throw new PrestoError(prestoResponse.error)
}

nextUri = prestoResponse?.nextUri
Expand Down
24 changes: 22 additions & 2 deletions presto-client/src/client.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ export interface PrestoResponse {
updateType: string
}

export interface PrestoError {
export interface PrestoErrorObject extends Error {
errorCode: number
errorName: string
errorType: string
failureInfo: unknown
failureInfo: {
message: string
stack: string[]
suppressed: string[]
type: string
}
message: string
}

Expand All @@ -54,3 +59,18 @@ export interface PrestoQuery {
export type GetPrestoDataParams = PrestoClientConfig & {
query: string
}

export class PrestoError extends Error implements PrestoErrorObject {
errorCode: number
errorName: string
errorType: string
failureInfo: PrestoErrorObject['failureInfo']

constructor({ errorCode, errorName, errorType, failureInfo, message }: PrestoErrorObject) {
super(message)
this.errorCode = errorCode
this.errorName = errorName
this.errorType = errorType
this.failureInfo = failureInfo
}
}

0 comments on commit 8221bdc

Please sign in to comment.