1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

added errors

This commit is contained in:
Rene Schallner 2023-03-10 11:43:57 +01:00
parent 0ec7fa4a91
commit 615d90b8f4
10 changed files with 51 additions and 39 deletions

View file

@ -219,7 +219,7 @@ fn on_request(r: zap.SimpleRequest) void {
if (r.query) |the_query| { if (r.query) |the_query| {
std.debug.print("QUERY: {s}\n", .{the_query}); std.debug.print("QUERY: {s}\n", .{the_query});
} }
_ = r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>"); r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
} }
pub fn main() !void { pub fn main() !void {

View file

@ -55,7 +55,7 @@ fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
if (userIdFromPath(path)) |id| { if (userIdFromPath(path)) |id| {
if (users.get(id)) |user| { if (users.get(id)) |user| {
if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| { 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; _ = e;
if (users.toJSON()) |json| { if (users.toJSON()) |json| {
_ = r.sendJson(json); defer alloc.free(json);
alloc.free(json); r.sendJson(json) catch return;
} else |err| { } else |err| {
std.debug.print("LIST error: {}\n", .{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| { if (users.addByName(u.first_name, u.last_name)) |id| {
var jsonbuf: [128]u8 = undefined; var jsonbuf: [128]u8 = undefined;
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
_ = r.sendJson(json); r.sendJson(json) catch return;
} }
} else |err| { } else |err| {
std.debug.print("ADDING error: {}\n", .{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; var jsonbuf: [128]u8 = undefined;
if (users.update(id, u.first_name, u.last_name)) { if (users.update(id, u.first_name, u.last_name)) {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
_ = r.sendJson(json); r.sendJson(json) catch return;
} }
} else { } else {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { 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; var jsonbuf: [128]u8 = undefined;
if (users.delete(id)) { if (users.delete(id)) {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
_ = r.sendJson(json); r.sendJson(json) catch return;
} }
} else { } else {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| {
_ = r.sendJson(json); r.sendJson(json) catch return;
} }
} }
} }

View file

@ -9,11 +9,11 @@ fn on_request_verbose(r: zap.SimpleRequest) void {
if (r.query) |the_query| { if (r.query) |the_query| {
std.debug.print("QUERY: {s}\n", .{the_query}); std.debug.print("QUERY: {s}\n", .{the_query});
} }
_ = r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>"); r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
} }
fn on_request_minimal(r: zap.SimpleRequest) void { fn on_request_minimal(r: zap.SimpleRequest) void {
_ = r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>"); r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
} }
pub fn main() !void { pub fn main() !void {

View file

@ -13,8 +13,8 @@ fn on_request(r: zap.SimpleRequest) void {
std.debug.print(">> BODY: {s}\n", .{the_body}); std.debug.print(">> BODY: {s}\n", .{the_body});
} }
r.setContentTypeFromPath(); r.setContentTypeFromPath() catch return;
_ = r.sendBody( r.sendBody(
\\ <html><body> \\ <html><body>
\\ <h1>Hello from ZAP!!!</h1> \\ <h1>Hello from ZAP!!!</h1>
\\ <form action="/" method="post"> \\ <form action="/" method="post">
@ -25,7 +25,7 @@ fn on_request(r: zap.SimpleRequest) void {
\\ <button>Send</button> \\ <button>Send</button>
\\ </form> \\ </form>
\\ </body></html> \\ </body></html>
); ) catch return;
} }
pub fn main() !void { pub fn main() !void {

View file

@ -26,8 +26,8 @@ fn on_request(r: zap.SimpleRequest) void {
json_to_send = "null"; json_to_send = "null";
} }
std.debug.print("<< json: {s}\n", .{json_to_send}); std.debug.print("<< json: {s}\n", .{json_to_send});
r.setContentType(.JSON); r.setContentType(.JSON) catch return;
_ = r.sendBody(json_to_send); r.sendBody(json_to_send) catch return;
} }
} }

View file

@ -25,11 +25,11 @@ fn on_request(r: zap.SimpleRequest) void {
}, },
}); });
defer ret.deinit(); defer ret.deinit();
r.setContentType(.TEXT); r.setContentType(.TEXT) catch return;
if (ret.str()) |s| { if (ret.str()) |s| {
_ = r.sendBody(s); r.sendBody(s) catch return;
} else { } else {
_ = r.sendBody("<html><body><h1>MustacheBuild() failed!</h1></body></html>"); r.sendBody("<html><body><h1>MustacheBuild() failed!</h1></body></html>") catch return;
} }
} }

View file

@ -10,18 +10,18 @@ fn dispatch_routes(r: zap.SimpleRequest) void {
} }
} }
// or default: present menu // or default: present menu
_ = r.sendBody( r.sendBody(
\\ <html> \\ <html>
\\ <body> \\ <body>
\\ <p><a href="/static">static</a></p> \\ <p><a href="/static">static</a></p>
\\ <p><a href="/dynamic">dynamic</a></p> \\ <p><a href="/dynamic">dynamic</a></p>
\\ </body> \\ </body>
\\ </html> \\ </html>
); ) catch return;
} }
fn static_site(r: zap.SimpleRequest) void { fn static_site(r: zap.SimpleRequest) void {
_ = r.sendBody("<html><body><h1>Hello from STATIC ZAP!</h1></body></html>"); r.sendBody("<html><body><h1>Hello from STATIC ZAP!</h1></body></html>") catch return;
} }
var dynamic_counter: i32 = 0; var dynamic_counter: i32 = 0;
@ -33,7 +33,7 @@ fn dynamic_site(r: zap.SimpleRequest) void {
"<html><body><h1>Hello # {d} from DYNAMIC ZAP!!!</h1></body></html>", "<html><body><h1>Hello # {d} from DYNAMIC ZAP!!!</h1></body></html>",
.{dynamic_counter}, .{dynamic_counter},
) catch "ERROR"; ) catch "ERROR";
_ = r.sendBody(filled_buf); r.sendBody(filled_buf) catch return;
} }
fn setup_routes(a: std.mem.Allocator) !void { fn setup_routes(a: std.mem.Allocator) !void {

View file

@ -3,7 +3,7 @@ const zap = @import("zap");
fn on_request(r: zap.SimpleRequest) void { fn on_request(r: zap.SimpleRequest) void {
r.setStatus(.not_found); r.setStatus(.not_found);
_ = r.sendBody("<html><body><h1>404 - File not found</h1></body></html>"); r.sendBody("<html><body><h1>404 - File not found</h1></body></html>") catch return;
} }
pub fn main() !void { pub fn main() !void {

View file

@ -1,4 +1,5 @@
pub const FIOBJ = usize; pub const FIOBJ = usize;
pub extern fn is_invalid(o: FIOBJ) c_int;
pub const fio_url_s = extern struct { pub const fio_url_s = extern struct {
scheme: fio_str_info_s, scheme: fio_str_info_s,
user: fio_str_info_s, user: fio_str_info_s,

View file

@ -43,6 +43,12 @@ pub const ListenError = error{
ListenError, ListenError,
}; };
pub const HttpError = error{
HttpSendBody,
HttpSetContentType,
HttpSetHeader,
};
pub const HttpParam = struct { pub const HttpParam = struct {
key: []const u8, key: []const u8,
value: []const u8, value: []const u8,
@ -63,25 +69,25 @@ pub const SimpleRequest = struct {
const Self = @This(); 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( const ret = fio.http_send_body(self.h, @intToPtr(
*anyopaque, *anyopaque,
@ptrToInt(body.ptr), @ptrToInt(body.ptr),
), body.len); ), body.len);
debug("SimpleRequest.sendBody(): ret = {}\n", .{ret}); 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)) { if (self.setContentType(.JSON)) {
return fio.http_send_body(self.h, @intToPtr( if (fio.http_send_body(self.h, @intToPtr(
*anyopaque, *anyopaque,
@ptrToInt(json.ptr), @ptrToInt(json.ptr),
), json.len); ), json.len) != 0) return error.HttpSendBody;
} else return error{ERR_CONTENT_TYPE}; } 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) { const s = switch (c) {
.TEXT => "text/plain", .TEXT => "text/plain",
.JSON => "application/json", .JSON => "application/json",
@ -96,25 +102,28 @@ pub const SimpleRequest = struct {
self: *const Self, self: *const Self,
c: ContentType, c: ContentType,
logger: *const Log, logger: *const Log,
) void { ) HttpError!void {
const s = switch (c) { const s = switch (c) {
.TEXT => "text/plain", .TEXT => "text/plain",
.JSON => "application/json", .JSON => "application/json",
else => "text/html", else => "text/html",
}; };
logger.log("setting content-type to {s}\n", .{s}); 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 { pub fn setContentTypeFromPath(self: *const Self) !void {
_ = fio.fiobj_hash_set( 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, self.h.*.private_data.out_headers,
fio.HTTP_HEADER_CONTENT_TYPE, 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 = .{ const hname: fio.fio_str_info_s = .{
.data = util.toCharPtr(name), .data = util.toCharPtr(name),
.len = name.len, .len = name.len,
@ -135,7 +144,8 @@ pub const SimpleRequest = struct {
// const new_fiobj_str = fio.fiobj_str_new(name.ptr, name.len); // const new_fiobj_str = fio.fiobj_str_new(name.ptr, name.len);
// fio.fiobj_free(new_fiobj_str); // 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 { 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 // 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( const ret = fio.http_send_body(request, @intToPtr(
*anyopaque, *anyopaque,
@ptrToInt(body.ptr), @ptrToInt(body.ptr),
), body.len); ), body.len);
debug("sendBody(): ret = {}\n", .{ret}); debug("sendBody(): ret = {}\n", .{ret});
if (ret != -1) return error.HttpSendBody;
} }