{"record":{"id":"dd039f5a2e4d5542","repo":"prestodb/presto","slug":"standarderrorcode-failureinfo-toexception-d","errorCode":null,"errorMessage":"${standardErrorCode}: failureInfo.toException() (decoded failure rethrown with supplied error code via fail() function)","messagePattern":"(.+?): failureInfo\\.toException\\(\\) \\(decoded failure rethrown with supplied error code via fail\\(\\) function\\)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/FailureFunction.java","lineNumber":57,"sourceCode":"    {\n        FailureInfo failureInfo = JSON_CODEC.fromJson(failureInfoSlice.getBytes());\n        // wrap the failure in a new exception to append the current stack trace\n        throw new PrestoException(StandardErrorCode.GENERIC_USER_ERROR, failureInfo.toException());\n    }\n\n    // This function is only used to propagate optimization failures.\n    @Description(\"Decodes json to an exception and throws it with supplied errorCode\")\n    @ScalarFunction(value = \"fail\", visibility = HIDDEN)\n    @SqlType(\"unknown\")\n    public static boolean failWithException(\n            @SqlType(StandardTypes.INTEGER) long errorCode,\n            @SqlType(StandardTypes.JSON) Slice failureInfoSlice)\n    {\n        FailureInfo failureInfo = JSON_CODEC.fromJson(failureInfoSlice.getBytes());\n        // wrap the failure in a new exception to append the current stack trace\n        for (StandardErrorCode standardErrorCode : StandardErrorCode.values()) {\n            if (standardErrorCode.toErrorCode().getCode() == errorCode) {\n                throw new PrestoException(standardErrorCode, failureInfo.toException());\n            }\n        }\n        throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, \"Unable to find error for code: \" + errorCode, failureInfo.toException());\n    }\n\n    @Description(\"Throws an exception with a given message\")\n    @ScalarFunction(value = \"fail\", visibility = HIDDEN)\n    @SqlType(\"unknown\")\n    public static boolean fail(@SqlType(StandardTypes.VARCHAR) Slice message)\n    {\n        throw new PrestoException(StandardErrorCode.GENERIC_USER_ERROR, message.toStringUtf8());\n    }\n\n    @Description(\"Throws an exception with a given error code and message\")\n    @ScalarFunction(value = \"fail\", visibility = HIDDEN)\n    @SqlType(\"unknown\")\n    public static boolean fail(\n            @SqlType(StandardTypes.INTEGER) long errorCode,","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/FailureFunction.java#L39-L75","documentation":"This overload of the hidden fail(json, code) function decodes a FailureInfo from JSON and re-throws it wrapped in a PrestoException using the caller-supplied StandardErrorCode. It allows an error captured during one phase to be re-raised with a specific error code chosen by the caller.","triggerScenarios":"Calling fail(json_failure_info, integer_error_code) where errorCode matches a valid StandardErrorCode value; the decoded exception is thrown under that code.","commonSituations":"Seen when engine-internal code defers a failure and rethrows it with a computed code, or when a developer calls fail() manually with a serialized failure and a numeric code such as 65536 (GENERIC_USER_ERROR).","solutions":["Inspect the inner decoded exception to fix the original problem.","Verify the numeric errorCode passed corresponds to the intended StandardErrorCode.","If you only want a generic user error, use the single-argument fail(json) form instead."],"exampleFix":"// before\nSELECT fail(failure_json, 999999)\n// after\nSELECT fail(failure_json, 65536) -- GENERIC_USER_ERROR","handlingStrategy":"validation","validationCode":"// caller-side check before invoking fail(json, code)\nboolean isValidCode = java.util.Arrays.stream(StandardErrorCode.values())\n    .anyMatch(c -> c.toErrorCode().getCode() == suppliedCode);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only pass numeric codes from the StandardErrorCode enum","Prefer the single-argument fail(json) unless a specific code is required","Centralize error-code constants instead of hardcoding numbers in SQL"],"tags":["sql","presto","error-propagation","error-code"],"backgroundTag":"deferred-query-error","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}