Skip to content

Commit a68819a

Browse files
committed
Computer-friendly errors
1 parent e830cb3 commit a68819a

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/tink/validation/Error.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package tink.validation;
2+
3+
enum Error {
4+
MissingField(field:String);
5+
UnexpectedType(expectedType:Dynamic, actualValue:Dynamic);
6+
}

src/tink/validation/macro/GenExtractor.hx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@ class GenExtractor {
1515
return macro if(value != null) $e else null;
1616

1717
static public function string()
18-
return macro if(!Std.is(value, String)) throw 'The value `' + value + '` should be String' else value;
18+
return macro if(!Std.is(value, String)) throw tink.validation.Error.UnexpectedType(String, value) else value;
1919

2020
static public function int()
21-
return macro if(!Std.is(value, Int)) throw 'The value `' + value + '` should be Int' else value;
21+
return macro if(!Std.is(value, Int)) throw tink.validation.Error.UnexpectedType(Int, value) else value;
2222

2323
static public function float()
24-
return macro if(!Std.is(value, Float)) throw 'The value `' + value + '` should be Float' else value;
24+
return macro if(!Std.is(value, Float)) throw tink.validation.Error.UnexpectedType(Float, value) else value;
2525

2626
static public function bool()
27-
return macro if(!Std.is(value, Bool)) throw 'The value `' + value + '` should be Bool' else value;
27+
return macro if(!Std.is(value, Bool)) throw tink.validation.Error.UnexpectedType(Bool, value) else value;
2828

2929
static public function date()
30-
return macro if(!Std.is(value, Date)) throw 'The value `' + value + '` should be Date' else value;
30+
return macro if(!Std.is(value, Date)) throw tink.validation.Error.UnexpectedType(Date, value) else value;
3131
// TODO: should make a copy? i.e. `Date.fromTime(value.getTime())`
3232

3333
static public function bytes()
34-
return macro if(!Std.is(value, Bytes)) throw 'The value `' + value + '` should be Bytes' else value;
34+
return macro if(!Std.is(value, haxe.io.Bytes)) throw tink.validation.Error.UnexpectedType(haxe.io.Bytes, value) else value;
3535

3636
static public function map(k, v)
37-
return macro if(!Std.is(value, Map)) throw 'The value `' + value + '` should be Map' else value;
37+
return macro if(!Std.is(value, Map)) throw tink.validation.Error.UnexpectedType(Map, value) else value;
3838

3939
static public function anon(fields:Array<FieldInfo>, ct)
4040
return (macro function (value:$ct) {
4141
var __ret:Dynamic = {};
4242
$b{[for(f in fields) {
4343
var name = f.name;
44-
var assert = f.optional ? macro null : macro if(!Reflect.hasField(value, $v{name})) throw $v{'Field `${f.name}` should not be null'};
44+
var assert = f.optional ? macro null : macro if(!Reflect.hasField(value, $v{name})) throw throw tink.validation.Error.MissingField($v{name});
4545
macro {
4646
$assert;
4747
var value = value.$name;
@@ -54,7 +54,7 @@ class GenExtractor {
5454
static public function array(e:Expr)
5555
{
5656
return macro {
57-
if(!Std.is(value, Array)) throw 'The value `' + value + '` should be Array';
57+
if(!Std.is(value, Array)) throw tink.validation.Error.UnexpectedType(Array, value);
5858
[for(value in (value:Array<Dynamic>)) $e];
5959
}
6060
}
@@ -68,7 +68,7 @@ class GenExtractor {
6868
ret;
6969
default: throw 'assert';
7070
}
71-
return macro if(!Std.is(value, $p{name})) throw 'The value `' + value + '` should be an EnumValue' else value;
71+
return macro if(!Std.is(value, $p{name})) throw tink.validation.Error.UnexpectedType($p{name}, value) else value;
7272
}
7373

7474
static public function dyn(_, _)

src/tink/validation/macro/GenValidator.hx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@ class GenValidator {
1515
return macro if(value != null) $e else null;
1616

1717
static public function string()
18-
return macro if(!Std.is(value, String)) throw 'The value `' + value + '` should be String';
18+
return macro if(!Std.is(value, String)) throw tink.validation.Error.UnexpectedType(String, value);
1919

2020
static public function int()
21-
return macro if(!Std.is(value, Int)) throw 'The value `' + value + '` should be Int';
21+
return macro if(!Std.is(value, Int)) throw tink.validation.Error.UnexpectedType(Int, value);
2222

2323
static public function float()
24-
return macro if(!Std.is(value, Float)) throw 'The value `' + value + '` should be Float';
24+
return macro if(!Std.is(value, Float)) throw tink.validation.Error.UnexpectedType(Float, value);
2525

2626
static public function bool()
27-
return macro if(!Std.is(value, Bool)) throw 'The value `' + value + '` should be Bool';
27+
return macro if(!Std.is(value, Bool)) throw tink.validation.Error.UnexpectedType(Bool, value);
2828

2929
static public function date()
30-
return macro if(!Std.is(value, Date)) throw 'The value `' + value + '` should be Date';
30+
return macro if(!Std.is(value, Date)) throw tink.validation.Error.UnexpectedType(Date, value);
3131

3232
static public function bytes()
33-
return macro if(!Std.is(value, Bytes)) throw 'The value `' + value + '` should be Bytes';
33+
return macro if(!Std.is(value, haxe.io.Bytes)) throw tink.validation.Error.UnexpectedType(haxe.io.Bytes, value);
3434

3535
static public function map(k, v)
36-
return macro if(!Std.is(value, Map)) throw 'The value `' + value + '` should be Map';
36+
return macro if(!Std.is(value, Map)) throw tink.validation.Error.UnexpectedType(Map, value);
3737

3838
static public function anon(fields:Array<FieldInfo>, ct)
3939
return (macro function (value:$ct) {
4040
$b{[for(f in fields) {
4141
var name = f.name;
42-
var assert = f.optional ? macro null : macro if(!Reflect.hasField(value, $v{name})) throw $v{'Field `${f.name}` should not be null'};
42+
var assert = f.optional ? macro null : macro if(!Reflect.hasField(value, $v{name})) throw throw tink.validation.Error.MissingField($v{name});
4343
macro {
4444
$assert;
4545
var value = value.$name;
@@ -52,7 +52,7 @@ class GenValidator {
5252
static public function array(e:Expr)
5353
{
5454
return macro {
55-
if(!Std.is(value, Array)) throw 'The value `' + value + '` should be Array';
55+
if(!Std.is(value, Array)) throw tink.validation.Error.UnexpectedType(Array, value);
5656
[for(value in (value:Array<Dynamic>)) $e];
5757
}
5858
}
@@ -66,7 +66,7 @@ class GenValidator {
6666
ret;
6767
default: throw 'assert';
6868
}
69-
return macro if(!Std.is(value, $p{name})) throw 'The value `' + value + '` should be an EnumValue';
69+
return macro if(!Std.is(value, $p{name})) throw tink.validation.Error.UnexpectedType($p{name}, value);
7070
}
7171

7272
static public function dyn(_, _)

0 commit comments

Comments
 (0)