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

Throw errors returned from the API instead of returning #62

Merged
merged 2 commits into from
Aug 25, 2022
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
31 changes: 6 additions & 25 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ describe('execute', () => {
headers: [':vtg1'],
types: { ':vtg1': 'INT32' },
rows: [{ ':vtg1': 1 }],
error: null,
size: 1,
statement: 'SELECT 1 from dual;',
time: 1,
Expand Down Expand Up @@ -179,7 +178,6 @@ describe('execute', () => {
rows: [],
rowsAffected: null,
insertId: null,
error: null,
size: 0,
statement: query,
time: 1
Expand Down Expand Up @@ -209,7 +207,6 @@ describe('execute', () => {
rows: [],
rowsAffected: 1,
insertId: null,
error: null,
size: 0,
statement: query,
time: 1
Expand Down Expand Up @@ -240,7 +237,6 @@ describe('execute', () => {
rows: [],
rowsAffected: 1,
insertId: '2',
error: null,
size: 0,
statement: query,
time: 1
Expand Down Expand Up @@ -287,7 +283,7 @@ describe('execute', () => {
}
})

test('it properly returns an error from the API', async () => {
test('it throws errors returned as a database error', async () => {
const mockError = {
message:
'target: test.0.primary: vttablet: rpc error: code = NotFound desc = Table \'vt_test_0.foo\' doesn\'t exist (errno 1146) (sqlstate 42S02) (CallerID: unsecure_grpc_client): Sql: "select * from foo", BindVars: {#maxLimit: "type:INT64 value:\\"10001\\""}',
Expand All @@ -301,23 +297,12 @@ describe('execute', () => {

mockPool.intercept({ path: EXECUTE_PATH, method: 'POST' }).reply(200, mockResponse)

const want: ExecutedQuery = {
headers: [],
types: {},
rows: [],
size: 0,
insertId: null,
rowsAffected: null,
error: mockError,
statement: 'SELECT * from foo;',
time: 1
}

const connection = connect(config)
const got = await connection.execute('SELECT * from foo;')
got.time = 1

expect(got).toEqual(want)
try {
await connection.execute('SELECT * from foo;')
} catch (err) {
expect(err).toEqual(new DatabaseError(mockError.message, 400, mockError))
}
})

test('it properly escapes query parameters', async () => {
Expand All @@ -334,7 +319,6 @@ describe('execute', () => {
rows: [{ ':vtg1': 1 }],
types: { ':vtg1': 'INT32' },
size: 1,
error: null,
insertId: null,
rowsAffected: null,
statement: "SELECT 1 from dual where foo = 'bar';",
Expand Down Expand Up @@ -368,7 +352,6 @@ describe('execute', () => {
types: { ':vtg1': 'INT32' },
rows: [{ ':vtg1': 1 }],
size: 1,
error: null,
insertId: null,
rowsAffected: null,
statement: 'select `login`, `email` from `users` where id = 42',
Expand Down Expand Up @@ -402,7 +385,6 @@ describe('execute', () => {
types: { ':vtg1': 'INT64' },
rows: [{ ':vtg1': BigInt(1) }],
size: 1,
error: null,
insertId: null,
rowsAffected: null,
statement: 'select 1 from dual',
Expand Down Expand Up @@ -439,7 +421,6 @@ describe('execute', () => {
types: { document: 'JSON' },
rows: [{ document: JSON.parse(document) }],
size: 1,
error: null,
insertId: null,
rowsAffected: null,
statement: 'select document from documents',
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export interface ExecutedQuery {
statement: string
insertId: string | null
rowsAffected: number | null
error: VitessError | null
time: number
}

Expand Down Expand Up @@ -183,6 +182,10 @@ export class Connection {
const time = Date.now() - start

const { result, session, error } = saved
if (error) {
throw new DatabaseError(error.message, 400, error)
}

const rowsAffected = result?.rowsAffected ? parseInt(result.rowsAffected, 10) : null
const insertId = result?.insertId ?? null

Expand All @@ -200,7 +203,6 @@ export class Connection {
rows,
rowsAffected,
insertId,
error: error ?? null,
size: rows.length,
statement: sql,
time
Expand Down