Repository navigation
missing errorCode and statusCode on errors (#33) #563
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { expect, AssertionError } from 'chai'; | ||
| import { DBSQLClient } from '../../lib'; | ||
| import IDBSQLSession from '../../lib/contracts/IDBSQLSession'; | ||
| import OperationStateError, { OperationStateErrorCode } from '../../lib/errors/OperationStateError'; | ||
|
|
||
| import config from './utils/config'; | ||
|
|
||
| // Regression for https://github.com/databricks/databricks-sql-nodejs/issues/33 — | ||
| // errors thrown for failed operations should carry a machine-readable code | ||
| // (SQLSTATE) at the top level, not only a message buried in `response`. | ||
| describe('Operation error codes', () => { | ||
| let client: DBSQLClient; | ||
| let session: IDBSQLSession; | ||
|
|
||
| before(async () => { | ||
| client = new DBSQLClient(); | ||
| const connection = await client.connect({ | ||
| host: config.host, | ||
| path: config.path, | ||
| token: config.token, | ||
| }); | ||
| session = await connection.openSession({ | ||
| initialCatalog: config.catalog, | ||
| initialSchema: config.schema, | ||
| }); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await session?.close(); | ||
| await client?.close(); | ||
| }); | ||
|
|
||
| async function expectOperationStateError(query: string) { | ||
| try { | ||
| const operation = await session.executeStatement(query); | ||
| await operation.fetchAll(); | ||
| expect.fail('It should throw an error'); | ||
| } catch (error) { | ||
| if (error instanceof AssertionError) { | ||
| throw error; | ||
| } | ||
| expect(error).to.be.instanceOf(OperationStateError); | ||
| return error as OperationStateError & { sqlState?: string }; | ||
| } | ||
| throw new Error('unreachable'); | ||
| } | ||
|
|
||
| it('should expose server SQLSTATE on OperationStateError for runtime failures', async () => { | ||
| const error = await expectOperationStateError("SELECT raise_error('boom')"); | ||
|
|
||
| expect(error.errorCode).to.be.eq(OperationStateErrorCode.Error); | ||
|
|
||
| const wireSqlState = error.response?.sqlState; | ||
| expect(wireSqlState, 'server should send a SQLSTATE on the wire').to.be.a('string'); | ||
| expect(wireSqlState).to.have.length(5); | ||
|
|
||
| expect(error.sqlState).to.be.eq(wireSqlState); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { expect } from 'chai'; | ||
| import OperationStateError, { OperationStateErrorCode } from '../../../lib/errors/OperationStateError'; | ||
| import { mapKernelErrorToJsError } from '../../../lib/kernel/KernelErrorMapping'; | ||
| import { TOperationState, TStatusCode } from '../../../thrift/TCLIService_types'; | ||
|
|
||
| describe('OperationStateError', () => { | ||
| it('should expose response.sqlState at top level', () => { | ||
| const error = new OperationStateError(OperationStateErrorCode.Error, { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS, sqlState: '00000' }, | ||
| operationState: TOperationState.ERROR_STATE, | ||
| sqlState: '22012', | ||
| displayMessage: 'Division by zero', | ||
| }) as OperationStateError & { sqlState?: string }; | ||
|
|
||
| expect(error.errorCode).to.be.eq(OperationStateErrorCode.Error); | ||
| expect(error.message).to.be.eq('Division by zero'); | ||
| expect(error.sqlState).to.be.eq('22012'); | ||
| }); | ||
|
|
||
| it('should not take sqlState from the RPC status', () => { | ||
| const error = new OperationStateError(OperationStateErrorCode.Error, { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS, sqlState: '00000' }, | ||
| operationState: TOperationState.ERROR_STATE, | ||
| }); | ||
|
|
||
| expect(Object.prototype.hasOwnProperty.call(error, 'sqlState')).to.be.false; | ||
| }); | ||
|
|
||
| it('should attach sqlState with the same descriptor as the kernel backend', () => { | ||
| const thriftError = new OperationStateError(OperationStateErrorCode.Error, { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| operationState: TOperationState.ERROR_STATE, | ||
| sqlState: '22012', | ||
| }); | ||
| const kernelError = mapKernelErrorToJsError({ code: 'SqlError', message: 'x', sqlstate: '22012' }); | ||
|
|
||
| expect(kernelError).to.be.instanceOf(OperationStateError); | ||
| expect(Object.getOwnPropertyDescriptor(thriftError, 'sqlState')).to.deep.eq( | ||
| Object.getOwnPropertyDescriptor(kernelError, 'sqlState'), | ||
| ); | ||
| expect(Object.keys(thriftError)).to.not.include('sqlState'); | ||
| expect(JSON.parse(JSON.stringify(thriftError))).to.not.have.property('sqlState'); | ||
| }); | ||
|
|
||
| it('should not define sqlState when the server reports none', () => { | ||
| const error = new OperationStateError(OperationStateErrorCode.Error, { | ||
| status: { statusCode: TStatusCode.SUCCESS_STATUS }, | ||
| operationState: TOperationState.ERROR_STATE, | ||
| }); | ||
|
|
||
| expect(Object.prototype.hasOwnProperty.call(error, 'sqlState')).to.be.false; | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.