diff options
author | 2023-07-11 12:48:32 -0700 | |
---|---|---|
committer | 2023-07-11 12:48:32 -0700 | |
commit | ae7bc37e94185726196a9cf77850379390904d4a (patch) | |
tree | 48467d82dc537c25a99eb6af355f5a3ce1cd72d8 | |
parent | 31ab56d36238528c6f44c52a6cbf600744a91f0a (diff) | |
download | bun-ae7bc37e94185726196a9cf77850379390904d4a.tar.gz bun-ae7bc37e94185726196a9cf77850379390904d4a.tar.zst bun-ae7bc37e94185726196a9cf77850379390904d4a.zip |
fix iterating stack trace (#3600)
* `i + 1` and remove `defer`
* a test
* fix test
-rw-r--r-- | src/bun.js/bindings/exports.zig | 6 | ||||
-rw-r--r-- | test/js/bun/http/error-response.js | 8 | ||||
-rw-r--r-- | test/js/bun/http/serve.test.ts | 15 |
3 files changed, 26 insertions, 3 deletions
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index db6de2ef3..e9e9d3a8d 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -460,7 +460,7 @@ pub const ZigStackTrace = extern struct { var source_line_len = source_lines_iter.getLength(); if (source_line_len > 0) { - var source_lines = try allocator.alloc(Api.SourceLine, @intCast(usize, @max(source_lines_iter.i, 0))); + var source_lines = try allocator.alloc(Api.SourceLine, @intCast(usize, @max(source_lines_iter.i + 1, 0))); var source_line_buf = try allocator.alloc(u8, source_line_len); source_lines_iter = this.sourceLineIterator(); var remain_buf = source_line_buf[0..]; @@ -468,7 +468,7 @@ pub const ZigStackTrace = extern struct { while (source_lines_iter.next()) |source| { const text = source.text.slice(); defer source.text.deinit(); - defer bun.copy( + bun.copy( u8, remain_buf, text, @@ -515,7 +515,7 @@ pub const ZigStackTrace = extern struct { pub fn getLength(this: *SourceLineIterator) usize { var count: usize = 0; - for (this.trace.source_lines_ptr[0..@intCast(usize, this.i)]) |*line| { + for (this.trace.source_lines_ptr[0..@intCast(usize, this.i + 1)]) |*line| { count += line.length(); } diff --git a/test/js/bun/http/error-response.js b/test/js/bun/http/error-response.js new file mode 100644 index 000000000..3284c146b --- /dev/null +++ b/test/js/bun/http/error-response.js @@ -0,0 +1,8 @@ +const s = Bun.serve({ + fetch(req, res) { + s.stop(true); + throw new Error("1"); + }, + port: 0, +}); +fetch(`http://${s.hostname}:${s.port}`).then(res => console.log(res.status)); diff --git a/test/js/bun/http/serve.test.ts b/test/js/bun/http/serve.test.ts index 7182ba68d..bba35c085 100644 --- a/test/js/bun/http/serve.test.ts +++ b/test/js/bun/http/serve.test.ts @@ -2,8 +2,10 @@ import { file, gc, Serve, serve, Server } from "bun"; import { afterEach, describe, it, expect, afterAll } from "bun:test"; import { readFileSync, writeFileSync } from "fs"; import { resolve } from "path"; +import { bunExe, bunEnv } from "harness"; import { renderToReadableStream } from "react-dom/server"; import app_jsx from "./app.jsx"; +import { spawn } from "child_process"; type Handler = (req: Request) => Response; afterEach(() => gc(true)); @@ -980,6 +982,19 @@ describe("should support Content-Range with Bun.file()", () => { } }); +it("formats error responses correctly", async () => { + const c = spawn(bunExe(), ["./error-response.js"], { cwd: import.meta.dir, env: bunEnv }); + + var output = ""; + c.stderr.on("data", chunk => { + output += chunk.toString(); + }); + c.stderr.on("end", () => { + expect(output).toContain('throw new Error("1");'); + c.kill(); + }); +}); + it("request body and signal life cycle", async () => { { const headers = { |