fail on auth failures

This commit is contained in:
Emil Lerch 2025-04-01 09:43:02 -07:00
parent a6de83b7c5
commit 0b810b0604
Signed by: lobo
GPG key ID: A7B62D657EF764F8
2 changed files with 17 additions and 5 deletions

View file

@ -8,7 +8,7 @@ const Args = struct {
syncthing_url: ?[]const u8 = null, syncthing_url: ?[]const u8 = null,
}; };
pub fn main() !void { pub fn main() !u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}).init; var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
@ -32,9 +32,19 @@ pub fn main() !void {
const arena = arena_alloc.allocator(); const arena = arena_alloc.allocator();
var poller = try EventPoller.init(arena, config); var poller = try EventPoller.init(arena, config);
const events = poller.poll() catch |err| { const events = poller.poll() catch |err| switch (err) {
error.Unauthorized => {
std.log.err("Not authorized to use syncthing. Please set ST_EVENTS_AUTH environment variable and try again", .{});
return 2;
},
error.MaxRetriesExceeded => {
std.log.err("Maximum retries exceeded - exiting", .{});
return 1;
},
else => {
std.log.err("Error polling events: {s}", .{@errorName(err)}); std.log.err("Error polling events: {s}", .{@errorName(err)});
continue; continue;
},
}; };
for (events) |event| { for (events) |event| {
@ -46,6 +56,7 @@ pub fn main() !void {
} }
} }
} }
return 0;
} }
fn parseArgs(allocator: std.mem.Allocator) !Args { fn parseArgs(allocator: std.mem.Allocator) !Args {

View file

@ -91,7 +91,7 @@ pub const EventPoller = struct {
const aa = arena.allocator(); const aa = arena.allocator();
try client.initDefaultProxies(aa); try client.initDefaultProxies(aa);
var retry_count: usize = self.config.max_retries; var retry_count: usize = 0;
while (retry_count < self.config.max_retries) : (retry_count += 1) { while (retry_count < self.config.max_retries) : (retry_count += 1) {
var url_buf: [1024]u8 = undefined; var url_buf: [1024]u8 = undefined;
var since_buf: [100]u8 = undefined; var since_buf: [100]u8 = undefined;
@ -118,6 +118,7 @@ pub const EventPoller = struct {
return err; return err;
}; };
if (response.status == .forbidden) return error.Unauthorized;
if (response.status != .ok) { if (response.status != .ok) {
std.log.err("HTTP status code: {}", .{response.status}); std.log.err("HTTP status code: {}", .{response.status});
if (retry_count + 1 < self.config.max_retries) { if (retry_count + 1 < self.config.max_retries) {