@@ -19,6 +19,7 @@ import { mocked } from "jest-mock";
1919import {
2020 anySignal ,
2121 ConnectionError ,
22+ HTTPError ,
2223 MatrixError ,
2324 parseErrorResponse ,
2425 retryNetworkOperation ,
@@ -113,6 +114,41 @@ describe("parseErrorResponse", () => {
113114 } , 500 ) ) ;
114115 } ) ;
115116
117+ it ( "should resolve Matrix Errors from XHR with urls" , ( ) => {
118+ expect ( parseErrorResponse ( {
119+ responseURL : "https://example.com" ,
120+ getResponseHeader ( name : string ) : string | null {
121+ return name === "Content-Type" ? "application/json" : null ;
122+ } ,
123+ status : 500 ,
124+ } as XMLHttpRequest , '{"errcode": "TEST"}' ) ) . toStrictEqual ( new MatrixError ( {
125+ errcode : "TEST" ,
126+ } , 500 , "https://example.com" ) ) ;
127+ } ) ;
128+
129+ it ( "should resolve Matrix Errors from fetch with urls" , ( ) => {
130+ expect ( parseErrorResponse ( {
131+ url : "https://example.com" ,
132+ headers : {
133+ get ( name : string ) : string | null {
134+ return name === "Content-Type" ? "application/json" : null ;
135+ } ,
136+ } ,
137+ status : 500 ,
138+ } as Response , '{"errcode": "TEST"}' ) ) . toStrictEqual ( new MatrixError ( {
139+ errcode : "TEST" ,
140+ } , 500 , "https://example.com" ) ) ;
141+ } ) ;
142+
143+ it ( "should set a sensible default error message on MatrixError" , ( ) => {
144+ let err = new MatrixError ( ) ;
145+ expect ( err . message ) . toEqual ( "MatrixError: Unknown message" ) ;
146+ err = new MatrixError ( {
147+ error : "Oh no" ,
148+ } ) ;
149+ expect ( err . message ) . toEqual ( "MatrixError: Oh no" ) ;
150+ } ) ;
151+
116152 it ( "should handle no type gracefully" , ( ) => {
117153 expect ( parseErrorResponse ( {
118154 headers : {
@@ -121,7 +157,7 @@ describe("parseErrorResponse", () => {
121157 } ,
122158 } ,
123159 status : 500 ,
124- } as Response , '{"errcode": "TEST"}' ) ) . toStrictEqual ( new Error ( "Server returned 500 error" ) ) ;
160+ } as Response , '{"errcode": "TEST"}' ) ) . toStrictEqual ( new HTTPError ( "Server returned 500 error" , 500 ) ) ;
125161 } ) ;
126162
127163 it ( "should handle invalid type gracefully" , ( ) => {
@@ -144,7 +180,7 @@ describe("parseErrorResponse", () => {
144180 } ,
145181 } ,
146182 status : 418 ,
147- } as Response , "I'm a teapot" ) ) . toStrictEqual ( new Error ( "Server returned 418 error: I'm a teapot" ) ) ;
183+ } as Response , "I'm a teapot" ) ) . toStrictEqual ( new HTTPError ( "Server returned 418 error: I'm a teapot" , 418 ) ) ;
148184 } ) ;
149185} ) ;
150186
0 commit comments