#Unable to catch Zod error

1 messages · Page 1 of 1 (latest)

tired pecan
#

Hi,

I'm creating a container builder system that intakes modal values from a user and attempts to create the selected component.
I am trying to use discordjs's builders to set the data, then run the .toJSON() function, since this will try to validate the builder's data. I wanted to catch this error and notify the user what they need to adjust, however I can receiving this and not sure how to proceed:

1125 | __name(uniqueArray, "uniqueArray");
1126 |
1127 | // src/lib/errors/CombinedPropertyError.ts
1128 | var _CombinedPropertyError = class _CombinedPropertyError extends BaseError {
1129 |   constructor(errors, validatorOptions) {
1130 |     super(validatorOptions?.message ?? "Received one or more errors");
                             ^
error: Received one or more errors
 errors: [
  [ "url", 756 | var BaseError = _BaseError;
757 |
758 | // src/lib/errors/BaseConstraintError.ts
759 | var _BaseConstraintError = class _BaseConstraintError extends BaseError {
760 |   constructor(constraint, message, given) {
761 |     super(message);
                            ^
error: Invalid protocol for media URL. Must be http:, https:, or attachment:
 constraint: "s.string().url()",
      given: "123123",
   expected: "expected to match a URL",

      at new BaseError (1:23)
      at new BaseConstraintError (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:761:5)
      at new ExpectedConstraintError (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:780:5)
      at run (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:2489:11)
      at run (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:962:27)
      at runPredicate (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:2099:32)
      at handleIgnoreStrategy (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:2109:9)
      at parse (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:972:90)
      at setURL (C:\Users\Blake\Desktop\discord\node_modules\@discordjs\builders\dist\index.js:1946:50)
      at add (C:\Users\Blake\Desktop\discord\src\lib\structures\template\strategies\MediaGalleryStrategy.ts:178:30)
 ]
],

      at new BaseError (1:23)
      at new CombinedPropertyError (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:1130:5)
      at handleIgnoreStrategy (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:2119:72)
      at parse (C:\Users\Blake\Desktop\discord\node_modules\@sapphire\shapeshift\dist\cjs\index.cjs:972:90)
      at setURL (C:\Users\Blake\Desktop\discord\node_modules\@discordjs\builders\dist\index.js:1946:50)
      at add (C:\Users\Blake\Desktop\discord\src\lib\structures\template\strategies\MediaGalleryStrategy.ts:178:30)

Bun v1.2.11 (Windows x64)
grand cedarBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

urban spindle
#

cant you just try-catch it? Also This is not Zod, builders stable release isnt on Zod yet.

tired pecan
#

I try that and it still throws that error before catching it, and the discordjs builder .toJSON() validator function returns a zod error, but the error thrown can’t be caught by my try and catch, super weird

#

  public validateBuilder(builder: JSONEncodable<any>): boolean {
    try {
      builder.toJSON()
    } catch (error) {
        console.log(error)
      return false
    }
    return true
  }

}

Sorry for any formatting issues I’m on mobile rn

#

The toJSON from StringSelectMenuOptionBuilder for example:

    /**
     * {@inheritDoc ComponentBuilder.toJSON}
     */
    public toJSON(validationOverride?: boolean): APISelectMenuOption {
        const clone = structuredClone(this.data);
        validate(selectMenuStringOptionPredicate, clone, validationOverride);

        return clone as APISelectMenuOption;
    }
import type { z } from 'zod';
import { fromZodError } from 'zod-validation-error';

let validationEnabled = true;

/**
 * Enables validators.
 *
 * @returns Whether validation is occurring.
 */
export function enableValidators() {
    return (validationEnabled = true);
}

/**
 * Disables validators.
 *
 * @returns Whether validation is occurring.
 */
export function disableValidators() {
    return (validationEnabled = false);
}

/**
 * Checks whether validation is occurring.
 */
export function isValidationEnabled() {
    return validationEnabled;
}

/**
 * Parses a value with a given validator, accounting for whether validation is enabled.
 *
 * @param validator - The zod validator to use
 * @param value - The value to parse
 * @param validationOverride - Force validation to run/not run regardless of your global preference
 * @returns The result from parsing
 * @internal
 */
export function validate<Validator extends z.ZodTypeAny>(
    validator: Validator,
    value: unknown,
    validationOverride?: boolean,
): z.output<Validator> {
    if (validationOverride === false || !isValidationEnabled()) {
        return value;
    }

    const result = validator.safeParse(value);

    if (!result.success) {
        throw fromZodError(result.error);
    }

    return result.data;
}

Just not sure why I can't catch that error and it's getting handled some other way through sapphire

urban spindle
#

again, it's not a zod error. Yes the master branch of discordjs builders has zod but that's not the version you're using. Look at your stack trace.

tired pecan
#

I still don't understand though why I can't catch this error within a try and catch

#

Ah I found my issue I'm stupid, I need to try and catch the editing of the builder, I thought I could just set it then validate with the .toJSON(), sorry for the confusion 😅

shut kindle
#

you will be able to once builders with zod releases

urban spindle
#

that's what ive been saying, that builders release isnt using zod yet

tired pecan
#

Yeah I was very confused with how their git worked 😅