@@ -244,7 +244,11 @@ function getSourceMapURL(
244244 if ( params . sourceMapURL === undefined || params . sourceMapURL === "" ) {
245245 return ;
246246 }
247- sourceMapURL = new URL ( params . sourceMapURL , params . url ) . toString ( ) ;
247+ // If sourceMapURL is relative
248+ sourceMapURL = new URL (
249+ params . sourceMapURL ,
250+ ! params . url . startsWith ( "script-" ) ? params . url : undefined
251+ ) . toString ( ) ;
248252 ws . close ( ) ;
249253 }
250254 } catch ( e ) {
@@ -277,32 +281,66 @@ async function getSources(inspectorBaseURL: URL, serviceName: string) {
277281 . sort ( ) ;
278282}
279283
280- // TODO(soon): just use `NoOpLog` when `Log#error()` no longer throws
281- class SafeLog extends Log {
282- error ( message : Error ) {
283- this . logWithLevel ( LogLevel . ERROR , String ( message ) ) ;
284+ class CustomLog extends Log {
285+ logs : [ LogLevel , string ] [ ] = [ ] ;
286+
287+ log ( message : string ) : void {
288+ this . logs . push ( [ LogLevel . NONE , message ] ) ;
289+ }
290+
291+ logWithLevel ( level : LogLevel , message : string ) {
292+ this . logs . push ( [ level , message ] ) ;
293+ }
294+
295+ getLogs ( level : LogLevel ) : string [ ] {
296+ return this . logs
297+ . filter ( ( [ logLevel ] ) => logLevel === level )
298+ . map ( ( [ , message ] ) => message ) ;
284299 }
285300}
286301
287302test ( "responds with pretty error page" , async ( t ) => {
303+ const log = new CustomLog ( ) ;
288304 const mf = new Miniflare ( {
289- log : new SafeLog ( LogLevel . NONE ) ,
305+ log,
290306 modules : true ,
291307 script : `
308+ function doSomething() {
309+ try {
310+ oops();
311+ } catch (e) {
312+ throw new Error("Unusual oops!", {
313+ cause: e,
314+ })
315+ }
316+ }
317+
318+ function oops() {
319+ throw new Error("Test error");
320+ }
321+
322+ // This emulates the reduceError function in the Wrangler middleware template
323+ // See packages/wrangler/templates/middleware/middleware-miniflare3-json-error.ts
292324 function reduceError(e) {
293325 return {
294326 name: e?.name,
295327 message: e?.message ?? String(e),
296328 stack: e?.stack,
329+ cause: e?.cause === undefined ? undefined : reduceError(e.cause),
297330 };
298331 }
332+
299333 export default {
300334 async fetch() {
301- const error = reduceError(new Error("Unusual oops!"));
302- return Response.json(error, {
303- status: 500,
304- headers: { "MF-Experimental-Error-Stack": "true" },
305- });
335+ try {
336+ doSomething();
337+ } catch (e) {
338+ const error = reduceError(e);
339+ return Response.json(error, {
340+ status: 500,
341+ headers: { "MF-Experimental-Error-Stack": "true" },
342+ });
343+ }
306344 }
307345 }` ,
308346 } ) ;
@@ -323,6 +361,18 @@ test("responds with pretty error page", async (t) => {
323361 t . regex ( text , / U R L .+ s o m e - u n u s u a l - p a t h / is) ;
324362 t . regex ( text , / X - U n u s u a l - K e y .+ s o m e - u n u s u a l - v a l u e / is) ;
325363
364+ // Check error logged
365+ const errorLogs = log . getLogs ( LogLevel . ERROR ) ;
366+ t . deepEqual ( errorLogs , [
367+ `Error: Unusual oops!
368+ at doSomething (script-0:6:12)
369+ at Object.fetch (script-0:30:6)
370+ Caused by: Error: Test error
371+ at oops (script-0:13:11)
372+ at doSomething (script-0:4:5)
373+ at Object.fetch (script-0:30:6)` ,
374+ ] ) ;
375+
326376 // Check `fetch()` accepting HTML returns pretty-error page
327377 res = await fetch ( url , { headers : { Accept : "text/html" } } ) ;
328378 t . is ( res . status , 500 ) ;
0 commit comments