@@ -1628,19 +1628,24 @@ class RawRemarkable implements RawRemarkableApi {
1628
1628
async getContent ( hash : string ) : Promise < Content > {
1629
1629
const raw = await this . getText ( hash ) ;
1630
1630
const loaded = JSON . parse ( raw ) as unknown ;
1631
+
1631
1632
// jtd can't verify non-discriminated unions, in this case, we have fileType
1632
- // defined or not. As a result, we only do a normal guard for the presence
1633
- // of tags (e.g. empty content or only specify tags). Otherwise we'll throw
1634
- // the full error for the richer content.
1635
- if ( collectionContent . guard ( loaded ) ) {
1636
- return loaded ;
1637
- } else if ( templateContent . guard ( loaded ) ) {
1638
- return loaded ;
1639
- } else if ( documentContent . guardAssert ( loaded ) ) {
1640
- return loaded ;
1641
- } else {
1642
- throw Error ( "invalid content" ) ;
1633
+ // defined or not. As a result, we try each, and concatenate the errors at the end
1634
+ const errors : string [ ] = [ ] ;
1635
+ for ( const [ name , valid ] of [
1636
+ [ "collection" , collectionContent ] ,
1637
+ [ "template" , templateContent ] ,
1638
+ [ "document" , documentContent ] ,
1639
+ ] as const ) {
1640
+ try {
1641
+ if ( valid . guardAssert ( loaded ) ) return loaded ;
1642
+ } catch ( ex ) {
1643
+ const msg = ex instanceof Error ? ex . message : "unknown error type" ;
1644
+ errors . push ( `Couldn't validate as ${ name } because:\n${ msg } ` ) ;
1645
+ }
1643
1646
}
1647
+ const joined = errors . join ( "\n\nor\n\n" ) ;
1648
+ throw new Error ( `invalid content: ${ joined } ` ) ;
1644
1649
}
1645
1650
1646
1651
async getMetadata ( hash : string ) : Promise < Metadata > {
0 commit comments