From 615d90b8f4628d47ce9a4d9e59413b0d22351bb9 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Fri, 10 Mar 2023 11:43:57 +0100 Subject: [PATCH] added errors --- README.md | 2 +- examples/endpoint/endpoint.zig | 16 ++++++------ examples/hello/hello.zig | 4 +-- examples/hello2/hello2.zig | 6 ++--- examples/hello_json/hello_json.zig | 4 +-- examples/mustache/mustache.zig | 6 ++--- examples/routes/routes.zig | 8 +++--- examples/serve/serve.zig | 2 +- src/fio.zig | 1 + src/zap.zig | 41 +++++++++++++++++++----------- 10 files changed, 51 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 701e360..a526103 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,7 @@ fn on_request(r: zap.SimpleRequest) void { if (r.query) |the_query| { std.debug.print("QUERY: {s}\n", .{the_query}); } - _ = r.sendBody("

Hello from ZAP!!!

"); + r.sendBody("

Hello from ZAP!!!

") catch return; } pub fn main() !void { diff --git a/examples/endpoint/endpoint.zig b/examples/endpoint/endpoint.zig index 3612a5b..aebd66a 100644 --- a/examples/endpoint/endpoint.zig +++ b/examples/endpoint/endpoint.zig @@ -55,7 +55,7 @@ fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { if (userIdFromPath(path)) |id| { if (users.get(id)) |user| { if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| { - _ = r.sendJson(json); + r.sendJson(json) catch return; } } } @@ -66,8 +66,8 @@ fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { _ = e; if (users.toJSON()) |json| { - _ = r.sendJson(json); - alloc.free(json); + defer alloc.free(json); + r.sendJson(json) catch return; } else |err| { std.debug.print("LIST error: {}\n", .{err}); } @@ -83,7 +83,7 @@ fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { if (users.addByName(u.first_name, u.last_name)) |id| { var jsonbuf: [128]u8 = undefined; if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { - _ = r.sendJson(json); + r.sendJson(json) catch return; } } else |err| { std.debug.print("ADDING error: {}\n", .{err}); @@ -106,11 +106,11 @@ fn putUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { var jsonbuf: [128]u8 = undefined; if (users.update(id, u.first_name, u.last_name)) { if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { - _ = r.sendJson(json); + r.sendJson(json) catch return; } } else { if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { - _ = r.sendJson(json); + r.sendJson(json) catch return; } } } @@ -127,11 +127,11 @@ fn deleteUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { var jsonbuf: [128]u8 = undefined; if (users.delete(id)) { if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { - _ = r.sendJson(json); + r.sendJson(json) catch return; } } else { if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { - _ = r.sendJson(json); + r.sendJson(json) catch return; } } } diff --git a/examples/hello/hello.zig b/examples/hello/hello.zig index e7b297c..738e345 100644 --- a/examples/hello/hello.zig +++ b/examples/hello/hello.zig @@ -9,11 +9,11 @@ fn on_request_verbose(r: zap.SimpleRequest) void { if (r.query) |the_query| { std.debug.print("QUERY: {s}\n", .{the_query}); } - _ = r.sendBody("

Hello from ZAP!!!

"); + r.sendBody("

Hello from ZAP!!!

") catch return; } fn on_request_minimal(r: zap.SimpleRequest) void { - _ = r.sendBody("

Hello from ZAP!!!

"); + r.sendBody("

Hello from ZAP!!!

") catch return; } pub fn main() !void { diff --git a/examples/hello2/hello2.zig b/examples/hello2/hello2.zig index 372046d..a4821a2 100644 --- a/examples/hello2/hello2.zig +++ b/examples/hello2/hello2.zig @@ -13,8 +13,8 @@ fn on_request(r: zap.SimpleRequest) void { std.debug.print(">> BODY: {s}\n", .{the_body}); } - r.setContentTypeFromPath(); - _ = r.sendBody( + r.setContentTypeFromPath() catch return; + r.sendBody( \\ \\

Hello from ZAP!!!

\\
@@ -25,7 +25,7 @@ fn on_request(r: zap.SimpleRequest) void { \\ \\
\\ - ); + ) catch return; } pub fn main() !void { diff --git a/examples/hello_json/hello_json.zig b/examples/hello_json/hello_json.zig index 0549d89..d23638c 100644 --- a/examples/hello_json/hello_json.zig +++ b/examples/hello_json/hello_json.zig @@ -26,8 +26,8 @@ fn on_request(r: zap.SimpleRequest) void { json_to_send = "null"; } std.debug.print("<< json: {s}\n", .{json_to_send}); - r.setContentType(.JSON); - _ = r.sendBody(json_to_send); + r.setContentType(.JSON) catch return; + r.sendBody(json_to_send) catch return; } } diff --git a/examples/mustache/mustache.zig b/examples/mustache/mustache.zig index 92a94fc..d6be8fb 100644 --- a/examples/mustache/mustache.zig +++ b/examples/mustache/mustache.zig @@ -25,11 +25,11 @@ fn on_request(r: zap.SimpleRequest) void { }, }); defer ret.deinit(); - r.setContentType(.TEXT); + r.setContentType(.TEXT) catch return; if (ret.str()) |s| { - _ = r.sendBody(s); + r.sendBody(s) catch return; } else { - _ = r.sendBody("

MustacheBuild() failed!

"); + r.sendBody("

MustacheBuild() failed!

") catch return; } } diff --git a/examples/routes/routes.zig b/examples/routes/routes.zig index 4d72891..5c4e7bd 100644 --- a/examples/routes/routes.zig +++ b/examples/routes/routes.zig @@ -10,18 +10,18 @@ fn dispatch_routes(r: zap.SimpleRequest) void { } } // or default: present menu - _ = r.sendBody( + r.sendBody( \\ \\ \\

static

\\

dynamic

\\ \\ - ); + ) catch return; } fn static_site(r: zap.SimpleRequest) void { - _ = r.sendBody("

Hello from STATIC ZAP!

"); + r.sendBody("

Hello from STATIC ZAP!

") catch return; } var dynamic_counter: i32 = 0; @@ -33,7 +33,7 @@ fn dynamic_site(r: zap.SimpleRequest) void { "

Hello # {d} from DYNAMIC ZAP!!!

", .{dynamic_counter}, ) catch "ERROR"; - _ = r.sendBody(filled_buf); + r.sendBody(filled_buf) catch return; } fn setup_routes(a: std.mem.Allocator) !void { diff --git a/examples/serve/serve.zig b/examples/serve/serve.zig index c5cbb62..117e553 100644 --- a/examples/serve/serve.zig +++ b/examples/serve/serve.zig @@ -3,7 +3,7 @@ const zap = @import("zap"); fn on_request(r: zap.SimpleRequest) void { r.setStatus(.not_found); - _ = r.sendBody("

404 - File not found

"); + r.sendBody("

404 - File not found

") catch return; } pub fn main() !void { diff --git a/src/fio.zig b/src/fio.zig index b17214b..98b63f0 100644 --- a/src/fio.zig +++ b/src/fio.zig @@ -1,4 +1,5 @@ pub const FIOBJ = usize; +pub extern fn is_invalid(o: FIOBJ) c_int; pub const fio_url_s = extern struct { scheme: fio_str_info_s, user: fio_str_info_s, diff --git a/src/zap.zig b/src/zap.zig index 51f33f5..d369f20 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -43,6 +43,12 @@ pub const ListenError = error{ ListenError, }; +pub const HttpError = error{ + HttpSendBody, + HttpSetContentType, + HttpSetHeader, +}; + pub const HttpParam = struct { key: []const u8, value: []const u8, @@ -63,25 +69,25 @@ pub const SimpleRequest = struct { const Self = @This(); - pub fn sendBody(self: *const Self, body: []const u8) c_int { + pub fn sendBody(self: *const Self, body: []const u8) HttpError!void { const ret = fio.http_send_body(self.h, @intToPtr( *anyopaque, @ptrToInt(body.ptr), ), body.len); debug("SimpleRequest.sendBody(): ret = {}\n", .{ret}); - return ret; + if (ret == -1) return error.HttpSendBody; } - pub fn sendJson(self: *const Self, json: []const u8) !c_int { + pub fn sendJson(self: *const Self, json: []const u8) HttpError!void { if (self.setContentType(.JSON)) { - return fio.http_send_body(self.h, @intToPtr( + if (fio.http_send_body(self.h, @intToPtr( *anyopaque, @ptrToInt(json.ptr), - ), json.len); - } else return error{ERR_CONTENT_TYPE}; + ), json.len) != 0) return error.HttpSendBody; + } else |err| return err; } - pub fn setContentType(self: *const Self, c: ContentType) bool { + pub fn setContentType(self: *const Self, c: ContentType) HttpError!void { const s = switch (c) { .TEXT => "text/plain", .JSON => "application/json", @@ -96,25 +102,28 @@ pub const SimpleRequest = struct { self: *const Self, c: ContentType, logger: *const Log, - ) void { + ) HttpError!void { const s = switch (c) { .TEXT => "text/plain", .JSON => "application/json", else => "text/html", }; logger.log("setting content-type to {s}\n", .{s}); - self.setHeader("content-type", s); + return self.setHeader("content-type", s); } - pub fn setContentTypeFromPath(self: *const Self) void { - _ = fio.fiobj_hash_set( + pub fn setContentTypeFromPath(self: *const Self) !void { + const t = fio.http_mimetype_find2(self.h.*.path); + if (fio.is_invalid(t) == 1) return error.HttpSetContentType; + const ret = fio.fiobj_hash_set( self.h.*.private_data.out_headers, fio.HTTP_HEADER_CONTENT_TYPE, - fio.http_mimetype_find2(self.h.*.path), + t, ); + if (ret == -1) return error.HttpSetContentType; } - pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) bool { + pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) HttpError!void { const hname: fio.fio_str_info_s = .{ .data = util.toCharPtr(name), .len = name.len, @@ -135,7 +144,8 @@ pub const SimpleRequest = struct { // const new_fiobj_str = fio.fiobj_str_new(name.ptr, name.len); // fio.fiobj_free(new_fiobj_str); - return ret == 0; + if (ret == 0) return; + return error.HttpSetHeader; } pub fn setStatusNumeric(self: *const Self, status: usize) void { @@ -319,10 +329,11 @@ pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSetti } // lower level sendBody -pub fn sendBody(request: [*c]fio.http_s, body: []const u8) void { +pub fn sendBody(request: [*c]fio.http_s, body: []const u8) HttpError!void { const ret = fio.http_send_body(request, @intToPtr( *anyopaque, @ptrToInt(body.ptr), ), body.len); debug("sendBody(): ret = {}\n", .{ret}); + if (ret != -1) return error.HttpSendBody; }