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

feat: provide better error messages when input param is invalid #1603

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
10 changes: 9 additions & 1 deletion src/data-types/tvp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type DataType } from '../data-type';
import { InputError } from '../errors';
import WritableTrackingBuffer from '../tracking-buffer/writable-tracking-buffer';

const TVP_ROW_TOKEN = Buffer.from([0x01]);
Expand Down Expand Up @@ -83,8 +84,15 @@ const TVP: DataType = {
const column = columns[k];
const value = row[k];

let paramValue;
try {
paramValue = column.type.validate(value, parameter.collation);
} catch (error) {
throw new InputError(`TVP column '${column.name}' has invalid data at row index ${i}`, { cause: error });
}

const param = {
value: column.type.validate(value, parameter.collation),
value: paramValue,
length: column.length,
scale: column.scale,
precision: column.precision
Expand Down
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ export class RequestError extends Error {
this.code = code;
}
}

export class InputError extends TypeError {}
7 changes: 6 additions & 1 deletion src/rpcrequest-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { writeToTrackingBuffer } from './all-headers';
import { type Parameter, type ParameterData } from './data-type';
import { type InternalConnectionOptions } from './connection';
import { Collation } from './collation';
import { InputError } from './errors';

// const OPTION = {
// WITH_RECOMPILE: 0x01,
Expand Down Expand Up @@ -113,7 +114,11 @@ class RpcRequestPayload implements Iterable<Buffer> {

yield type.generateTypeInfo(param, this.options);
yield type.generateParameterLength(param, this.options);
yield * type.generateParameterData(param, this.options);
try {
yield * type.generateParameterData(param, this.options);
} catch (error) {
throw new InputError(`Input parameter '${parameter.name}' could not be validated`, { cause: error });
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions test/integration/tvp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Request from '../../src/request';
import { debugOptionsFromEnv } from '../helpers/debug-options-from-env';

import defaultConfig from '../config';
import { InputError } from '../../src/errors';

function getConfig() {
const config = {
Expand Down Expand Up @@ -181,8 +182,14 @@ describe('calling a procedure that takes and returns a TVP', function() {

it('correctly handles validation errors', function(done) {
const request = new Request('__tediousTvpTest', (err) => {
assert.instanceOf(err, TypeError);
assert.strictEqual(err?.message, 'Value must be between 0 and 255, inclusive.');
assert.instanceOf(err, InputError);
assert.strictEqual(err?.message, 'Input parameter \'tvp\' could not be validated');

assert.instanceOf(err?.cause, InputError);
assert.strictEqual(/** @type {InputError} */(err?.cause).message, 'TVP column \'b\' has invalid data at row index 0');

assert.instanceOf(/** @type {InputError} */(err?.cause).cause, TypeError);
assert.strictEqual(/** @type {TypeError} */(/** @type {InputError} */(err?.cause).cause).message, 'Value must be between 0 and 255, inclusive.');

const request = new Request('SELECT 1', done);
connection.execSql(request);
Expand Down
Loading