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

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/data-types/tvp.ts
@@ -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 @@ -92,7 +93,11 @@ const TVP: DataType = {

// TvpColumnData
yield column.type.generateParameterLength(param, options);
yield * column.type.generateParameterData(param, options);
try {
yield * column.type.generateParameterData(param, options);
} catch (error) {
throw new InputError(`TVP column i=${k} has invalid data at row j=${i}`, error);
}
}
}

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

export class InputError extends TypeError {
constructor(message: string, readonly cause: unknown) {
super(
message + (cause instanceof Error ? `. Details: ${cause.message}` : '')
);
}
}
7 changes: 6 additions & 1 deletion src/rpcrequest-payload.ts
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 with name=${parameter.name} has invalid data`, error);
}
}
}

Expand Down