@@ -2,24 +2,45 @@ import { CustomError } from 'ts-custom-error';
22import { ErrorPolykey , sysexits } from '../errors' ;
33
44/**
5- * This packages the `ErrorParse` array into the `data` property
6- * This is to allow encoding to and decoding from GRPC errors
5+ * Generic error containing all parsing errors that occurred during
6+ * execution.
77 */
88class ErrorValidation extends ErrorPolykey {
9- public readonly errors : Array < ErrorParse > ;
10- constructor ( errors : Array < ErrorParse > ) {
9+ description = 'Input data failed validation' ;
10+ exitCode = sysexits . DATAERR ;
11+ public errors : Array < ErrorParse > ;
12+ constructor ( message , data ) {
13+ super ( message , data ) ;
14+ if ( data . errors != null ) {
15+ const errors : Array < ErrorParse > = [ ] ;
16+ for ( const eData of data . errors ) {
17+ const errorParse = new ErrorParse ( eData . message ) ;
18+ errorParse . keyPath = eData . keyPath ;
19+ errorParse . value = eData . value ;
20+ errorParse . context = eData . context ;
21+ errors . push ( errorParse ) ;
22+ }
23+ this . errors = errors ;
24+ }
25+ }
26+
27+ /**
28+ * This packages an `ErrorParse` array into the `data` property
29+ * This is to allow encoding to and decoding from GRPC errors
30+ */
31+ static createFromErrors ( errors : Array < ErrorParse > ) : ErrorValidation {
1132 const message = errors . map ( ( e ) => e . message ) . join ( '; ' ) ;
1233 const data = {
1334 errors : errors . map ( ( e ) => ( {
1435 message : e . message ,
1536 keyPath : e . keyPath ,
1637 value : e . value . valueOf ( ) ,
38+ context : e . context ,
1739 } ) ) ,
1840 } ;
19- super ( message , data ) ;
20- this . description = 'Input data failed validation' ;
21- this . exitCode = sysexits . DATAERR ;
22- this . errors = errors ;
41+ const e = new ErrorValidation ( message , data ) ;
42+ e . errors = errors ;
43+ return e ;
2344 }
2445}
2546
0 commit comments