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

Refactor compiler error codes #1189

Open
pront opened this issue Dec 12, 2024 · 0 comments
Open

Refactor compiler error codes #1189

pront opened this issue Dec 12, 2024 · 0 comments
Labels
meta: good first issue Anything that is good for new contributors type: tech debt A code change that does not add user value vrl: compiler Changes to the compiler

Comments

@pront
Copy link
Member

pront commented Dec 12, 2024

The Problem

Currently we assign arbitrary error codes in different modules e.g.

    fn code(&self) -> usize {
        // ...
        match self {
            Undefined { .. } => 105,
            WrongNumberOfArgs { .. } => 106,
            UnknownKeyword { .. } => 108,
            Compilation { .. } => 610,
            // ...
        }
    }

These codes are scattered across different files and there's nothing preventing us from reusing them.

Solution

We should aggregate these under a single enum in src/compiler/errors.

Without giving this too much thought (might revisit later), we can have static arrays of codes:

const FUNCTION_CALL_ERROR_CODES: &[(FunctionCallError, usize)] = &[
    (Undefined, 105),
    (WrongNumberOfArgs, 106),
    (UnknownKeyword, 108),
    (Compilation, 610),
    // ...
];

and then use them like so:

impl DiagnosticMessage for FunctionCallError {
    fn code(&self) -> usize {
        use FunctionCallError::*;

        FUNCTION_CALL_ERROR_CODES
            .iter()
            .find(|&&(ref variant, _)| variant == self)
            .map(|&(_, code)| code)
            .expect("Error code not found for FunctionCallError variant")
    }
}

Docs

If we find duplicates and have to re-assign codes, we will also need to update:
https://vector.dev/docs/reference/vrl/errors/#compile-time-errors

Changing codes assigned to errors IMO isn't a breaking change.

To VRL users, if you use these codes, please comment in this ticket!

References

Ideally these should be generated from code, see #280.

@pront pront added type: tech debt A code change that does not add user value meta: good first issue Anything that is good for new contributors vrl: compiler Changes to the compiler labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta: good first issue Anything that is good for new contributors type: tech debt A code change that does not add user value vrl: compiler Changes to the compiler
Projects
None yet
Development

No branches or pull requests

1 participant