mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
fix(Bun.serve): return EACCESS when we don't have perms (#7191)
* fix(Bun.serve): return EACCESS when we don't have perms The error reported to js land when listening fails was always the same, this adds a second one for EACCESS when we are not the super user. Fixes: https://github.com/oven-sh/bun/issues/7187 * fix: adjust code to be only ran on linuz * fix: correct typo * fix: remove comment since its linux only now
This commit is contained in:
@@ -14,6 +14,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext;
|
||||
const Fs = @import("../../fs.zig");
|
||||
const Resolver = @import("../../resolver/resolver.zig");
|
||||
const ast = @import("../../import_record.zig");
|
||||
const Sys = @import("../../sys.zig");
|
||||
|
||||
const MacroEntryPoint = bun.bundler.MacroEntryPoint;
|
||||
const logger = @import("root").bun.logger;
|
||||
@@ -5601,11 +5602,25 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp
|
||||
if (error_instance == .zero) {
|
||||
switch (this.config.address) {
|
||||
.tcp => |tcp| {
|
||||
error_instance = (JSC.SystemError{
|
||||
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "Failed to start server. Is port {d} in use?", .{tcp.port}) catch "Failed to start server"),
|
||||
.code = bun.String.static("EADDRINUSE"),
|
||||
.syscall = bun.String.static("listen"),
|
||||
}).toErrorInstance(this.globalThis);
|
||||
error_set: {
|
||||
if (comptime Environment.isLinux) {
|
||||
var rc: i32 = -1;
|
||||
const code = Sys.getErrno(rc);
|
||||
if (code == bun.C.E.ACCES) {
|
||||
error_instance = (JSC.SystemError{
|
||||
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "permission denied {s}:{d}", .{ tcp.hostname orelse "0.0.0.0", tcp.port }) catch "Failed to start server"),
|
||||
.code = bun.String.static("EACCES"),
|
||||
.syscall = bun.String.static("listen"),
|
||||
}).toErrorInstance(this.globalThis);
|
||||
break :error_set;
|
||||
}
|
||||
}
|
||||
error_instance = (JSC.SystemError{
|
||||
.message = bun.String.init(std.fmt.bufPrint(&output_buf, "Failed to start server. Is port {d} in use?", .{tcp.port}) catch "Failed to start server"),
|
||||
.code = bun.String.static("EADDRINUSE"),
|
||||
.syscall = bun.String.static("listen"),
|
||||
}).toErrorInstance(this.globalThis);
|
||||
}
|
||||
},
|
||||
.unix => |unix| {
|
||||
error_instance = (JSC.SystemError{
|
||||
|
||||
@@ -1304,3 +1304,14 @@ it("should response with HTTP 413 when request body is larger than maxRequestBod
|
||||
|
||||
server.stop(true);
|
||||
});
|
||||
if (process.platform === "linux")
|
||||
it("should use correct error when using a root range port(#7187)", () => {
|
||||
expect(() => {
|
||||
const server = Bun.serve({
|
||||
port: 1003,
|
||||
fetch(req) {
|
||||
return new Response("request answered");
|
||||
},
|
||||
});
|
||||
}).toThrow("permission denied 0.0.0.0:1003");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user