Skip to content

Commit

Permalink
feat: provide better error messages when input param is invalid (#1603)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Sun <47126816+MichaelSun90@users.noreply.github.com>
  • Loading branch information
jtomaszewski and MichaelSun90 authored Jul 31, 2024
1 parent 3856dc5 commit 209b9f3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
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

0 comments on commit 209b9f3

Please sign in to comment.