ref: d95be792d6b5e4a2f9f1615a40492020c764ee30
parent: 36a8d961bf90a5b30e7fc9dedb53f07d485ef41f
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Jun 1 16:55:48 EDT 2025
acmed: parse and show error messages returned from the server Now that we have support in webfs for accessing the error body, we can show the reason that a request fails in acmed. This is useful for debugging why renewing a certificate failed.
--- a/sys/src/cmd/auth/acmed.c
+++ b/sys/src/cmd/auth/acmed.c
@@ -152,7 +152,7 @@
b[*n] = 0;
return b;
}
-
+
static int
webopen(char *url, char *dir, int ndir)
{
@@ -175,6 +175,39 @@
return -1;
}
+static void
+weberror(char *dir)
+{
+ char *m, *r, path[80];
+ int fd, nr;
+ JSON *j;
+ JSONEl *e;
+
+ snprint(path, sizeof(path), "%s/%s", dir, "errorbody");
+ if((fd = open(path, OREAD|OCEXEC)) == -1)
+ abort();
+ if((r = slurp(fd, &nr)) != nil){
+ m = r;
+ if(debug)
+ goto Rawdump;
+ if((j = jsonparse(r)) == nil)
+ goto Rawdump;
+ if(j->t != JSONObject)
+ goto Rawdump;
+ for(e = j->first; e != nil; e = e->next){
+ if(e->val->t == JSONString && strcmp(e->name, "detail") == 0){
+ m = e->val->s;
+ break;
+ }
+ }
+Rawdump:
+ fprint(2, "%s\n", m);
+ free(r);
+ }else
+ fprint(2, "error recovering error: %r");
+ close(fd);
+}
+
static char*
get(char *url, int *n)
{
@@ -186,8 +219,10 @@
if((cfd = webopen(url, dir, sizeof(dir))) == -1)
goto Error;
snprint(path, sizeof(path), "%s/%s", dir, "body");
- if((dfd = open(path, OREAD|OCEXEC)) == -1)
+ if((dfd = open(path, OREAD|OCEXEC)) == -1){
+ weberror(dir);
goto Error;
+ }
r = slurp(dfd, n);
Error:
if(dfd != -1) close(dfd);
@@ -216,8 +251,10 @@
goto Error;
close(dfd);
snprint(path, sizeof(path), "%s/%s", dir, "body");
- if((dfd = open(path, OREAD|OCEXEC)) == -1)
+ if((dfd = open(path, OREAD|OCEXEC)) == -1){
+ weberror(dir);
goto Error;
+ }
if(h != nil){
snprint(path, sizeof(path), "%s/%s", dir, h->name);
if((hfd = open(path, OREAD|OCEXEC)) == -1)
--
⑨