Kevin Kostlan
Kevin Kostlan

Reputation: 3519

haxe macro catching of Context.parse() errors

At compile (macro) time, calling

Context.parse("a bad expression", somePos) 

produces an error that cannot be caught in a try-catch block (Edit: this is wrong, see below). Is there a way to catch this error? Context.parseInlineString() doesn't seem to work either.

Others functions such as Context.typeExpr() have a similar problem.

Edit: The type of catch was wrong. I did:

try {...}
catch (err:String) {...}

What you have to do:

try {...}
catch (err:Dynamic) {...}

Careful reading of the documentation explains this. This is different than Java for which there is one type of exception per error. In Haxe, most errors are strings, but there are some others like the one here.

Upvotes: 1

Views: 178

Answers (1)

Andy Li
Andy Li

Reputation: 6034

The following works for me:

import haxe.macro.*;

class Test {
    macro static function test() {
        try {
            Context.parse("a bad expression", Context.currentPos());
        } catch(e:Dynamic) {
            trace(e); //Test.hx:8: Missing ;
        }
        return macro {};
    }
    static function main() {
        test();
    }
}

Upvotes: 3

Related Questions