diff --git a/packages/core/src/common/queries/errors.ts b/packages/core/src/common/queries/errors.ts new file mode 100644 index 0000000..b6eb7e5 --- /dev/null +++ b/packages/core/src/common/queries/errors.ts @@ -0,0 +1,4 @@ + +export class DeserializeError extends Error {} + +export class SerializeError extends Error {} diff --git a/packages/core/src/common/queries/index.ts b/packages/core/src/common/queries/index.ts index 70ffab8..3567d32 100644 --- a/packages/core/src/common/queries/index.ts +++ b/packages/core/src/common/queries/index.ts @@ -1,2 +1,3 @@ export * from './common'; +export * from './errors'; export * as queryMediaTypes from './media-types'; diff --git a/packages/core/src/common/queries/media-types/application/x-www-form-urlencoded.ts b/packages/core/src/common/queries/media-types/application/x-www-form-urlencoded.ts index 72b42e5..984a576 100644 --- a/packages/core/src/common/queries/media-types/application/x-www-form-urlencoded.ts +++ b/packages/core/src/common/queries/media-types/application/x-www-form-urlencoded.ts @@ -5,6 +5,11 @@ import { QueryOrGrouping, } from '../../common'; +import { + DeserializeError, + SerializeError, +} from '../../errors'; + interface ProcessEntryBase { type: string; } @@ -34,10 +39,12 @@ interface ProcessEntryBoolean extends ProcessEntryBase { truthyStrings?: string[]; } -export type ProcessEntry = ProcessEntryString | ProcessEntryNumber | ProcessEntryBoolean; +type ProcessEntry = ProcessEntryString | ProcessEntryNumber | ProcessEntryBoolean; export const name = 'application/x-www-form-urlencoded' as const; +class DeserializeInvalidFormatError extends DeserializeError {} + const normalizeRhs = (lhs: string, rhs: string, processEntriesMap?: Record) => { const defaultCoerceValues = { type: 'string' @@ -107,7 +114,7 @@ const normalizeRhs = (lhs: string, rhs: string, processEntriesMap?: Record; - throw new Error(`Invalid coercion type: ${unknownCoerceValues.type}`); + throw new DeserializeInvalidFormatError(`Invalid coercion type: ${unknownCoerceValues.type}`); // this will be sent to the data source, e.g., the SQL query // we can also make this function act as a "sanitizer" } @@ -186,6 +193,8 @@ export const deserialize: QueryMediaType< ) }; +class SerializeInvalidExpressionError extends SerializeError {} + const serializeExpression = (ex2: QueryAnyExpression) => { if ('name' in ex2) { return [ex2.name, `(${ex2.args.map((s) => s.toString()).join(',')})`]; @@ -193,7 +202,7 @@ const serializeExpression = (ex2: QueryAnyExpression) => { if (ex2.rhs instanceof RegExp) { if (ex2.operator !== 'REGEXP') { - throw new Error(`Invalid rhs given for operator: ${ex2.lhs} ${ex2.operator} `); + throw new SerializeInvalidExpressionError(`Invalid rhs given for operator: ${ex2.lhs} ${ex2.operator} `); } return [ex2.lhs, ex2.rhs.toString()]; @@ -210,7 +219,7 @@ const serializeExpression = (ex2: QueryAnyExpression) => { default: break; } - throw new Error(`Invalid operator given for lhs: ${ex2.lhs} ${ex2.rhs}`); + throw new SerializeInvalidExpressionError(`Invalid operator given for lhs: ${ex2.lhs} ${ex2.rhs}`); } case 'number': { return [ex2.lhs, ex2.rhs.toString()]; @@ -222,7 +231,7 @@ const serializeExpression = (ex2: QueryAnyExpression) => { break; } - throw new Error(`Unknown type for rhs: ${ex2.lhs} ${ex2.operator} `); + throw new SerializeInvalidExpressionError(`Unknown type for rhs: ${ex2.lhs} ${ex2.operator} `); }; export const serialize: QueryMediaType<