Mirror for Friday Night Funkin
at main 75 lines 2.2 kB view raw
1package funkin.data; 2 3import json2object.Position; 4import json2object.Position.Line; 5import json2object.Error; 6 7class DataError 8{ 9 public static function printError(error:Error):Void 10 { 11 switch (error) 12 { 13 case IncorrectType(vari, expected, pos): 14 trace(' Expected field "$vari" to be of type "$expected".'); 15 printPos(pos); 16 case IncorrectEnumValue(value, expected, pos): 17 trace(' Invalid enum value (expected "$expected", got "$value")'); 18 printPos(pos); 19 case InvalidEnumConstructor(value, expected, pos): 20 trace(' Invalid enum constructor (epxected "$expected", got "$value")'); 21 printPos(pos); 22 case UninitializedVariable(vari, pos): 23 trace(' Uninitialized variable "$vari"'); 24 printPos(pos); 25 case UnknownVariable(vari, pos): 26 trace(' Unknown variable "$vari"'); 27 printPos(pos); 28 case ParserError(message, pos): 29 trace(' Parsing error: ${message}'); 30 printPos(pos); 31 case CustomFunctionException(e, pos): 32 if (Std.isOfType(e, String)) 33 { 34 trace(' ${e}'); 35 } 36 else 37 { 38 printUnknownError(e); 39 } 40 printPos(pos); 41 default: 42 printUnknownError(error); 43 } 44 } 45 46 public static function printUnknownError(e:Dynamic):Void 47 { 48 switch (Type.typeof(e)) 49 { 50 case TClass(c): 51 trace(' [${Type.getClassName(c)}] ${e.toString()}'); 52 case TEnum(c): 53 trace(' [${Type.getEnumName(c)}] ${e.toString()}'); 54 default: 55 trace(' [${Type.typeof(e)}] ${e.toString()}'); 56 } 57 } 58 59 /** 60 * TODO: Figure out the nicest way to print this. 61 * Maybe look up how other JSON parsers format their errors? 62 * @see https://github.com/elnabo/json2object/blob/master/src/json2object/Position.hx 63 */ 64 static function printPos(pos:Position):Void 65 { 66 if (pos.lines[0].number == pos.lines[pos.lines.length - 1].number) 67 { 68 trace(' at ${(pos.file == '') ? 'line ' : '${pos.file}:'}${pos.lines[0].number}'); 69 } 70 else 71 { 72 trace(' at ${(pos.file == '') ? 'line ' : '${pos.file}:'}${pos.lines[0].number}-${pos.lines[pos.lines.length - 1].number}'); 73 } 74 } 75}