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: add new error type for param validation #1566

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/bulk-load.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from 'events';
import WritableTrackingBuffer from './tracking-buffer/writable-tracking-buffer';
import { ParameterValidationError } from './errors';
import Connection, { type InternalConnectionOptions } from './connection';

import { Transform } from 'stream';
Expand Down Expand Up @@ -185,7 +186,8 @@ class RowTransform extends Transform {
try {
value = c.type.validate(value, c.collation);
} catch (error: any) {
return callback(error);
const validateError = new ParameterValidationError(error.message, c.name, value);
return callback(validateError);
}
}

Expand Down
29 changes: 15 additions & 14 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import SqlBatchPayload from './sqlbatch-payload';
import MessageIO from './message-io';
import { Parser as TokenStreamParser } from './token/token-stream-parser';
import { Transaction, ISOLATION_LEVEL, assertValidIsolationLevel } from './transaction';
import { ConnectionError, RequestError } from './errors';
import { ConnectionError, RequestError, ParameterValidationError } from './errors';
import { connectInParallel, connectInSequence } from './connector';
import { name as libraryName } from './library';
import { versions } from './tds-versions';
Expand Down Expand Up @@ -2829,26 +2829,27 @@ class Connection extends EventEmitter {
scale: undefined
});

try {
for (let i = 0, len = request.parameters.length; i < len; i++) {
const parameter = request.parameters[i];

for (let i = 0, len = request.parameters.length; i < len; i++) {
const parameter = request.parameters[i];
const value = parameters ? parameters[parameter.name] : null;
try {
executeParameters.push({
...parameter,
value: parameter.type.validate(parameters ? parameters[parameter.name] : null, this.databaseCollation)
value: parameter.type.validate(value, this.databaseCollation)
});
}
} catch (error: any) {
request.error = error;
} catch (error: any) {
const validateError = new ParameterValidationError(error.message, parameter.name, value);
request.error = validateError;

process.nextTick(() => {
this.debug.log(error.message);
request.callback(error);
});
process.nextTick(() => {
this.debug.log(validateError.message);
request.callback(validateError);
});

return;
return;
}
}

this.makeRequest(request, TYPE.RPC_REQUEST, new RpcRequestPayload(Procedures.Sp_Execute, executeParameters, this.currentTransactionDescriptor(), this.config.options, this.databaseCollation));
}

Expand Down
11 changes: 11 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ export class RequestError extends Error {
this.code = code;
}
}

export class ParameterValidationError extends TypeError {
paramName: string | undefined;
paramValue: any | undefined;

constructor(message: string, paramName: string, paramValue: any) {
super(message);
this.paramName = paramName;
this.paramValue = paramValue;
}
}
8 changes: 6 additions & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import { RequestError, ParameterValidationError } from './errors';
import { type Parameter, type DataType } from './data-type';
import { RequestError } from './errors';

import Connection from './connection';
import { type Metadata } from './metadata-parser';
Expand Down Expand Up @@ -463,7 +463,11 @@ class Request extends EventEmitter {
try {
parameter.value = parameter.type.validate(parameter.value, collation);
} catch (error: any) {
throw new RequestError('Validation failed for parameter \'' + parameter.name + '\'. ' + error.message, 'EPARAM');
const paramvalidationErr = new ParameterValidationError(error.message, parameter.name, parameter.value);
const requestErr = new RequestError('Validation failed for parameter \'' + parameter.name + '\'. ' + error.message, 'EPARAM');
requestErr.cause = paramvalidationErr;

throw requestErr;
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions test/integration/bulk-load-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const assert = require('chai').assert;
const TYPES = require('../../src/data-type').typeByName;

import Connection from '../../src/connection';
import { RequestError } from '../../src/errors';
import { RequestError, ParameterValidationError } from '../../src/errors';
import Request from '../../src/request';
import { debugOptionsFromEnv } from '../helpers/debug-options-from-env';

Expand Down Expand Up @@ -1517,8 +1517,10 @@ describe('BulkLoad', function() {
* @param {undefined | number} rowCount
*/
function completeBulkLoad(err, rowCount) {
assert.instanceOf(err, TypeError);
assert.strictEqual(/** @type {TypeError} */(err).message, 'Invalid date.');
assert.instanceOf(err, ParameterValidationError);
assert.strictEqual(/** @type {ParameterValidationError} */(err).paramName, 'value');
assert.strictEqual(/** @type {ParameterValidationError} */(err).paramValue, 'invalid date');
assert.strictEqual(/** @type {ParameterValidationError} */(err).message, 'Validation failed for parameter:"value" with value:"invalid date" and message:"Invalid date."');

done();
}
Expand All @@ -1537,8 +1539,10 @@ describe('BulkLoad', function() {
* @param {undefined | number} rowCount
*/
function completeBulkLoad(err, rowCount) {
assert.instanceOf(err, TypeError);
assert.strictEqual(/** @type {TypeError} */(err).message, 'Invalid date.');
assert.instanceOf(err, ParameterValidationError);
assert.strictEqual(/** @type {ParameterValidationError} */(err).paramName, 'value');
assert.strictEqual(/** @type {ParameterValidationError} */(err).paramValue, 'invalid date');
assert.strictEqual(/** @type {ParameterValidationError} */(err).message, 'Validation failed for parameter:"value" with value:"invalid date" and message:"Invalid date."');

assert.strictEqual(rowCount, 0);

Expand Down
Loading