-
Notifications
You must be signed in to change notification settings - Fork 1
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
Error getting RGB++ assets through assets api #210
Comments
The IssueThe issue arises because we didn't add enough restrictions/validations to the
Validate the params as we do when validating Bitcoin addressesCurrently, the Bitcoin address params are validated as follows: const { address } = request.params;
const valid = validateBitcoinAddress(address);
if (!valid) {
throw fastify.httpErrors.badRequest('Invalid Bitcoin address');
} When an invalid address is passed, an error will be thrown: {
"message": "Invalid Bitcoin address"
} We can add more custom validation for other params, like so: const { txid } = request.params;
validateHashAndThrow(txid); Add validators to the params schema and prompt the errors correctlyWe can add validators to the params schema: params: z.object({
hash: z.string().describe('The Bitcoin block hash').length(64, 'Invalid hash'),
}), Then, an error will be thrown when requesting the {
"message": "[\n {\n \"code\": \"too_small\",\n \"minimum\": 64,\n \"type\": \"string\",\n \"inclusive\": true,\n \"exact\": true,\n \"message\": \"Invalid hash\",\n \"path\": [\n \"hash\"\n ]\n }\n]"
} The above error message is a stringified JSON, which does not look good. However, we could add error handling logic for the fastify.setErrorHandler((error, request, reply) => {
if (error.name === 'ZodError') {
try {
const json = JSON.parse(error.message);
reply.status(400).send({
message: 'Zod validation failed',
error: json,
});
} catch {
//
}
}
reply.status(400).send(error);
}); The formatted error will be presented as: {
"message": "Zod validation failed",
"error": [
{
"code": "too_small",
"minimum": 64,
"type": "string",
"inclusive": true,
"exact": true,
"message": "Invalid hash",
"path": [
"hash"
]
}
]
} |
@ahonn suggests that we should only prompt the first schema parse error instead of throwing all the errors at once. A solution is to use a custom import { serializerCompiler } from 'fastify-type-provider-zod';
const validatorCompiler: FastifySchemaCompiler<ZodAny> =
({ schema }) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(data): any => {
try {
return { value: schema.parse(data) };
} catch (error) {
if (error instanceof ZodError) {
return {
error: new Error(error.errors[0].message),
};
}
return { error };
}
};
app.setValidatorCompiler(validatorCompiler); |
'/rgbpp/v1/assets/{btc_txid}/{vout}' Data can still be obtained even if the parameter is empty
Response:
The text was updated successfully, but these errors were encountered: