malloydata / malloy

Malloy is an experimental language for describing data relationships and transformations.
http://www.malloydata.dev
MIT License
2k stars 76 forks source link

Add error codes #1904

Closed christopherswenson closed 2 months ago

christopherswenson commented 2 months ago

This PR adds an error code for every error/warning emitted by the translator.

New error codes should be added to parse-log.ts in the MessageParameterTypes type, with either 'the-error-code': string or 'the-error-code': { value: number, ...otherProperties }.

Errors can be logged like:

someMalloyElement.logError(
  'the-error-code', 
  `Oh no, you messed up! ${someValue} is illegal`
);

or

someMalloyElement.logError('the-error-code', {value: someValue});

A third argument may be passed, which is an object that can have:

logWarning also exists for logging warnings...

If using the latter style, be sure to add a message formatter into parse-log.ts in the MESSAGE_FORMATTERS object:

'the-error-code': e => `Oh no, you messed up! ${e.value} is illegal`,

The error formatter can return a plain string message, or an object that can have:

There was also a pattern of doing:

this.log('This does not work');
return errorFor('this-does-not-work');

This now becomes:

return this.loggedErrorExpr('this-does-not-work', {});

Testing for log messages has changed from

expect(something).translationToFailWith(
  `Oh no, you messed up! 42 is illegal`
)

to

expect(something).toLog(error('the-error-code'))

If you want to check that the properties of the error are set correctly, you can do

expect(something).toLog(error('the-error-code', {value: 42}))

If you explicitly want to check that the message is formatted correctly, do

expect(something).toLog(errorMessage(`Oh no, you messed up! 42 is illegal`))