@@ -596,7 +596,13 @@ class FsFileRead<
596
596
* ```
597
597
*/
598
598
public async text ( ) {
599
- return fs . readFile ( this . _path , this . _encoding ) ;
599
+ try {
600
+ return await fs . readFile ( this . _path , this . _encoding ) ;
601
+ } catch ( error ) {
602
+ throw new Error ( `Failed to read text from ${ this . _path } ` , {
603
+ cause : error ,
604
+ } ) ;
605
+ }
600
606
}
601
607
602
608
/**
@@ -612,7 +618,13 @@ class FsFileRead<
612
618
* ```
613
619
*/
614
620
public textSync ( ) {
615
- return fsSync . readFileSync ( this . _path , this . _encoding ) ;
621
+ try {
622
+ return fsSync . readFileSync ( this . _path , this . _encoding ) ;
623
+ } catch ( error ) {
624
+ throw new Error ( `Failed to read text from ${ this . _path } ` , {
625
+ cause : error ,
626
+ } ) ;
627
+ }
616
628
}
617
629
618
630
/**
@@ -675,9 +687,13 @@ class FsFileRead<
675
687
public json <
676
688
OUT = SCHEMA extends ZodSchema < infer O > ? O : unknown ,
677
689
> ( ) : Promise < OUT > {
678
- return this . text ( ) . then ( ( t ) =>
679
- json . deserialize ( t , { schema : this . _schema } ) ,
680
- ) ;
690
+ return this . text ( )
691
+ . then ( ( t ) => json . deserialize ( t , { schema : this . _schema } ) )
692
+ . catch ( ( error ) => {
693
+ throw new Error ( `Failed to read JSON from ${ this . _path } ` , {
694
+ cause : error ,
695
+ } ) ;
696
+ } ) ;
681
697
}
682
698
683
699
/**
@@ -699,9 +715,15 @@ class FsFileRead<
699
715
public jsonSync <
700
716
OUT = SCHEMA extends ZodSchema < infer O > ? O : unknown ,
701
717
> ( ) : OUT {
702
- return json . deserialize ( this . textSync ( ) , {
703
- schema : this . _schema ,
704
- } ) ;
718
+ try {
719
+ return json . deserialize ( this . textSync ( ) , {
720
+ schema : this . _schema ,
721
+ } ) ;
722
+ } catch ( error ) {
723
+ throw new Error ( `Failed to read JSON from ${ this . _path } ` , {
724
+ cause : error ,
725
+ } ) ;
726
+ }
705
727
}
706
728
707
729
/**
@@ -728,9 +750,13 @@ class FsFileRead<
728
750
public yaml <
729
751
OUT = SCHEMA extends ZodSchema < infer O > ? O : unknown ,
730
752
> ( ) : Promise < OUT > {
731
- return this . text ( ) . then ( ( t ) =>
732
- yaml . deserialize ( t , { schema : this . _schema } ) ,
733
- ) ;
753
+ return this . text ( )
754
+ . then ( ( t ) => yaml . deserialize ( t , { schema : this . _schema } ) )
755
+ . catch ( ( error ) => {
756
+ throw new Error ( `Failed to read YAML from ${ this . _path } ` , {
757
+ cause : error ,
758
+ } ) ;
759
+ } ) ;
734
760
}
735
761
736
762
/**
@@ -753,9 +779,15 @@ class FsFileRead<
753
779
public yamlSync <
754
780
OUT = SCHEMA extends ZodSchema < infer O > ? O : unknown ,
755
781
> ( ) : OUT {
756
- return yaml . deserialize ( this . textSync ( ) , {
757
- schema : this . _schema ,
758
- } ) ;
782
+ try {
783
+ return yaml . deserialize ( this . textSync ( ) , {
784
+ schema : this . _schema ,
785
+ } ) ;
786
+ } catch ( error ) {
787
+ throw new Error ( `Failed to read YAML from ${ this . _path } ` , {
788
+ cause : error ,
789
+ } ) ;
790
+ }
759
791
}
760
792
761
793
/**
@@ -785,7 +817,13 @@ class FsFileRead<
785
817
* - Does not support all XML features (see documentation for details)
786
818
*/
787
819
public async xml < T extends Array < Xml . Node > > ( ) : Promise < T > {
788
- return this . text ( ) . then ( ( content ) => xml . parse < T > ( content ) ) ;
820
+ return this . text ( )
821
+ . then ( ( content ) => xml . parse < T > ( content ) )
822
+ . catch ( ( error ) => {
823
+ throw new Error ( `Failed to read XML from ${ this . _path } ` , {
824
+ cause : error ,
825
+ } ) ;
826
+ } ) ;
789
827
}
790
828
791
829
/**
@@ -810,7 +848,13 @@ class FsFileRead<
810
848
* - Does not support all XML features (see documentation for details)
811
849
*/
812
850
public xmlSync < T extends Array < Xml . Node > > ( ) : T {
813
- return xml . parse < T > ( this . textSync ( ) ) ;
851
+ try {
852
+ return xml . parse < T > ( this . textSync ( ) ) ;
853
+ } catch ( error ) {
854
+ throw new Error ( `Failed to read XML from ${ this . _path } ` , {
855
+ cause : error ,
856
+ } ) ;
857
+ }
814
858
}
815
859
816
860
/**
@@ -830,7 +874,13 @@ class FsFileRead<
830
874
* ```
831
875
*/
832
876
public async base64 ( ) : Promise < string > {
833
- return fs . readFile ( this . _path , "base64" ) ;
877
+ try {
878
+ return await fs . readFile ( this . _path , "base64" ) ;
879
+ } catch ( error ) {
880
+ throw new Error ( `Failed to read base64 from ${ this . _path } ` , {
881
+ cause : error ,
882
+ } ) ;
883
+ }
834
884
}
835
885
836
886
/**
@@ -847,7 +897,13 @@ class FsFileRead<
847
897
* ```
848
898
*/
849
899
public base64Sync ( ) : string {
850
- return fsSync . readFileSync ( this . _path , "base64" ) ;
900
+ try {
901
+ return fsSync . readFileSync ( this . _path , "base64" ) ;
902
+ } catch ( error ) {
903
+ throw new Error ( `Failed to read base64 from ${ this . _path } ` , {
904
+ cause : error ,
905
+ } ) ;
906
+ }
851
907
}
852
908
853
909
/**
@@ -921,11 +977,17 @@ class FsFileRead<
921
977
* @throws If schema validation fails when a schema is provided
922
978
*/
923
979
public md < DATA_SHAPE = SCHEMA extends ZodSchema < infer O > ? O : unknown > ( ) {
924
- return this . text ( ) . then ( ( t ) =>
925
- MdDoc . withOptions < DATA_SHAPE > ( {
926
- schema : this . _schema ,
927
- } ) . fromString ( t ) ,
928
- ) ;
980
+ return this . text ( )
981
+ . then ( ( t ) =>
982
+ MdDoc . withOptions < DATA_SHAPE > ( {
983
+ schema : this . _schema ,
984
+ } ) . fromString ( t ) ,
985
+ )
986
+ . catch ( ( error ) => {
987
+ throw new Error ( `Failed to read markdown from ${ this . _path } ` , {
988
+ cause : error ,
989
+ } ) ;
990
+ } ) ;
929
991
}
930
992
931
993
/**
@@ -940,9 +1002,15 @@ class FsFileRead<
940
1002
public mdSync <
941
1003
DATA_SHAPE = SCHEMA extends ZodSchema < infer O > ? O : unknown ,
942
1004
> ( ) {
943
- return MdDoc . withOptions < DATA_SHAPE > ( {
944
- schema : this . _schema ,
945
- } ) . fromString ( this . textSync ( ) ) ;
1005
+ try {
1006
+ return MdDoc . withOptions < DATA_SHAPE > ( {
1007
+ schema : this . _schema ,
1008
+ } ) . fromString ( this . textSync ( ) ) ;
1009
+ } catch ( error ) {
1010
+ throw new Error ( `Failed to read markdown from ${ this . _path } ` , {
1011
+ cause : error ,
1012
+ } ) ;
1013
+ }
946
1014
}
947
1015
}
948
1016
@@ -990,8 +1058,14 @@ class FsFileWrite<
990
1058
if ( this . _mode === "preserve" && ( await FsFile . from ( this . _path ) . exists ( ) ) )
991
1059
return ;
992
1060
const dirname = path . dirname ( this . _path ) ;
993
- await fs . mkdir ( dirname , { recursive : true } ) ;
994
- await fs . writeFile ( this . _path , content . toString ( ) , this . _encoding ) ;
1061
+ try {
1062
+ await fs . mkdir ( dirname , { recursive : true } ) ;
1063
+ await fs . writeFile ( this . _path , content . toString ( ) , this . _encoding ) ;
1064
+ } catch ( error ) {
1065
+ throw new Error ( `Failed to write text to ${ this . _path } ` , {
1066
+ cause : error ,
1067
+ } ) ;
1068
+ }
995
1069
}
996
1070
997
1071
/**
@@ -1007,8 +1081,14 @@ class FsFileWrite<
1007
1081
if ( this . _mode === "preserve" && FsFile . from ( this . _path ) . existsSync ( ) )
1008
1082
return ;
1009
1083
const dirname = path . dirname ( this . _path ) ;
1010
- fsSync . mkdirSync ( dirname , { recursive : true } ) ;
1011
- fsSync . writeFileSync ( this . _path , content . toString ( ) , this . _encoding ) ;
1084
+ try {
1085
+ fsSync . mkdirSync ( dirname , { recursive : true } ) ;
1086
+ fsSync . writeFileSync ( this . _path , content . toString ( ) , this . _encoding ) ;
1087
+ } catch ( error ) {
1088
+ throw new Error ( `Failed to write text to ${ this . _path } ` , {
1089
+ cause : error ,
1090
+ } ) ;
1091
+ }
1012
1092
}
1013
1093
1014
1094
/**
@@ -1024,7 +1104,13 @@ class FsFileWrite<
1024
1104
public async json < T > (
1025
1105
data : TSchema extends ZodSchema < any , infer I > ? I : T ,
1026
1106
) : Promise < void > {
1027
- return this . text ( json . serialize ( data , { schema : this . _schema } ) ) ;
1107
+ return this . text ( json . serialize ( data , { schema : this . _schema } ) ) . catch (
1108
+ ( error ) => {
1109
+ throw new Error ( `Failed to write JSON to ${ this . _path } ` , {
1110
+ cause : error ,
1111
+ } ) ;
1112
+ } ,
1113
+ ) ;
1028
1114
}
1029
1115
1030
1116
// Todo: add mergeJson
@@ -1044,7 +1130,11 @@ class FsFileWrite<
1044
1130
) : Promise < void > {
1045
1131
return this . text (
1046
1132
json . serialize ( data , { schema : this . _schema , pretty : true } ) + "\n" ,
1047
- ) ;
1133
+ ) . catch ( ( error ) => {
1134
+ throw new Error ( `Failed to write pretty JSON to ${ this . _path } ` , {
1135
+ cause : error ,
1136
+ } ) ;
1137
+ } ) ;
1048
1138
}
1049
1139
1050
1140
/**
@@ -1060,7 +1150,13 @@ class FsFileWrite<
1060
1150
public jsonSync < T > (
1061
1151
data : TSchema extends ZodSchema < any , infer I > ? I : T ,
1062
1152
) : void {
1063
- return this . textSync ( json . serialize ( data , { schema : this . _schema } ) ) ;
1153
+ try {
1154
+ return this . textSync ( json . serialize ( data , { schema : this . _schema } ) ) ;
1155
+ } catch ( error ) {
1156
+ throw new Error ( `Failed to write JSON to ${ this . _path } ` , {
1157
+ cause : error ,
1158
+ } ) ;
1159
+ }
1064
1160
}
1065
1161
1066
1162
/**
@@ -1076,9 +1172,15 @@ class FsFileWrite<
1076
1172
public prettyJsonSync < T = unknown > (
1077
1173
data : TSchema extends ZodSchema < any , infer I > ? I : T ,
1078
1174
) : void {
1079
- return this . textSync (
1080
- json . serialize ( data , { schema : this . _schema , pretty : true } ) + "\n" ,
1081
- ) ;
1175
+ try {
1176
+ return this . textSync (
1177
+ json . serialize ( data , { schema : this . _schema , pretty : true } ) + "\n" ,
1178
+ ) ;
1179
+ } catch ( error ) {
1180
+ throw new Error ( `Failed to write pretty JSON to ${ this . _path } ` , {
1181
+ cause : error ,
1182
+ } ) ;
1183
+ }
1082
1184
}
1083
1185
1084
1186
/**
@@ -1093,7 +1195,13 @@ class FsFileWrite<
1093
1195
public async yaml < T = unknown > (
1094
1196
data : TSchema extends ZodSchema < any , infer I > ? I : T ,
1095
1197
) : Promise < void > {
1096
- return this . text ( yaml . serialize ( data , { schema : this . _schema } ) ) ;
1198
+ return this . text ( yaml . serialize ( data , { schema : this . _schema } ) ) . catch (
1199
+ ( error ) => {
1200
+ throw new Error ( `Failed to write YAML to ${ this . _path } ` , {
1201
+ cause : error ,
1202
+ } ) ;
1203
+ } ,
1204
+ ) ;
1097
1205
}
1098
1206
1099
1207
/**
@@ -1109,7 +1217,13 @@ class FsFileWrite<
1109
1217
public yamlSync < T = unknown > (
1110
1218
data : TSchema extends ZodSchema < any , infer I > ? I : T ,
1111
1219
) : void {
1112
- return this . textSync ( yaml . serialize ( data , { schema : this . _schema } ) ) ;
1220
+ try {
1221
+ return this . textSync ( yaml . serialize ( data , { schema : this . _schema } ) ) ;
1222
+ } catch ( error ) {
1223
+ throw new Error ( `Failed to write YAML to ${ this . _path } ` , {
1224
+ cause : error ,
1225
+ } ) ;
1226
+ }
1113
1227
}
1114
1228
1115
1229
/**
@@ -1124,8 +1238,14 @@ class FsFileWrite<
1124
1238
if ( this . _mode === "preserve" && ( await FsFile . from ( this . _path ) . exists ( ) ) )
1125
1239
return ;
1126
1240
const dirname = path . dirname ( this . _path ) ;
1127
- await fs . mkdir ( dirname , { recursive : true } ) ;
1128
- await fs . writeFile ( this . _path , data . toString ( ) , "base64" ) ;
1241
+ try {
1242
+ await fs . mkdir ( dirname , { recursive : true } ) ;
1243
+ await fs . writeFile ( this . _path , data . toString ( ) , "base64" ) ;
1244
+ } catch ( error ) {
1245
+ throw new Error ( `Failed to write base64 to ${ this . _path } ` , {
1246
+ cause : error ,
1247
+ } ) ;
1248
+ }
1129
1249
}
1130
1250
1131
1251
/**
@@ -1141,8 +1261,14 @@ class FsFileWrite<
1141
1261
if ( this . _mode === "preserve" && FsFile . from ( this . _path ) . existsSync ( ) )
1142
1262
return ;
1143
1263
const dirname = path . dirname ( this . _path ) ;
1144
- fsSync . mkdirSync ( dirname , { recursive : true } ) ;
1145
- return fsSync . writeFileSync ( this . _path , data . toString ( ) , "base64" ) ;
1264
+ try {
1265
+ fsSync . mkdirSync ( dirname , { recursive : true } ) ;
1266
+ fsSync . writeFileSync ( this . _path , data . toString ( ) , "base64" ) ;
1267
+ } catch ( error ) {
1268
+ throw new Error ( `Failed to write base64 to ${ this . _path } ` , {
1269
+ cause : error ,
1270
+ } ) ;
1271
+ }
1146
1272
}
1147
1273
1148
1274
/**
@@ -1154,7 +1280,11 @@ class FsFileWrite<
1154
1280
* @throws If schema validation fails or if the write operation fails
1155
1281
*/
1156
1282
public md ( data : MdDoc ) {
1157
- return this . text ( data . toMd ( ) ) ;
1283
+ return this . text ( data . toMd ( ) ) . catch ( ( error ) => {
1284
+ throw new Error ( `Failed to write markdown to ${ this . _path } ` , {
1285
+ cause : error ,
1286
+ } ) ;
1287
+ } ) ;
1158
1288
}
1159
1289
1160
1290
/**
@@ -1167,7 +1297,13 @@ class FsFileWrite<
1167
1297
* @synchronous
1168
1298
*/
1169
1299
public mdSync ( data : MdDoc ) {
1170
- return this . textSync ( data . toMd ( ) ) ;
1300
+ try {
1301
+ return this . textSync ( data . toMd ( ) ) ;
1302
+ } catch ( error ) {
1303
+ throw new Error ( `Failed to write markdown to ${ this . _path } ` , {
1304
+ cause : error ,
1305
+ } ) ;
1306
+ }
1171
1307
}
1172
1308
}
1173
1309
0 commit comments