Error-handling for invalid requests etc

This commit is contained in:
Jaidyn Ann 2023-07-28 06:59:25 -05:00
parent b34ca289a3
commit 5a2562fd07

View File

@ -71,12 +71,21 @@ in the docstring of SERVER."
type "application/activity+json"))))) type "application/activity+json")))))
(define-condition invalid-acct-uri (error) ()
(:documentation "Thrown when a user@host acct URI is expected, but is lacking a user or host potion."))
(defun resource-user-host (resource) (defun resource-user-host (resource)
"Given a queried RESOURCE, return a list of its contained user and host." "Given a queried RESOURCE, return a list of its contained user and host."
(let* ((sans-acct (if (str:starts-with-p "acct:" resource) (let* ((sans-acct (if (str:starts-with-p "acct:" resource)
(subseq resource 5) (subseq resource 5)
resource))) resource))
(str:split #\@ sans-acct))) (user-host (str:split #\@ sans-acct)))
(if (or (not (eq (length user-host) 2))
(str:emptyp (car user-host))
(str:emptyp (cadr user-host)))
(error 'invalid-acct-uri :message "Invalid acct resource")
user-host)))
(defun filter-link-rels (rels link-plists) (defun filter-link-rels (rels link-plists)
@ -109,14 +118,25 @@ the RESOURCE and RELS parameters from a Webfinger HTTP request, return the
response JSON in Clacks format. response JSON in Clacks format.
This can be used if you dont want to wrap your server with SERVER, and would This can be used if you dont want to wrap your server with SERVER, and would
rather handle the Webfinger path yourself." rather handle the Webfinger path yourself."
(list 200 (list
'(:content-type "text/plain") 200
(list (format nil "~A~%" '(:content-type "text/plain")
(apply #'user-json (list
(filter-user-info-rels (format
rels nil "~A"
(apply user-info-func (handler-case
(resource-user-host resource)))))))) (if (not resource)
"\"No resource specified\""
(or (apply #'user-json
(filter-user-info-rels
rels
(apply user-info-func
(resource-user-host resource))))
"\"Couldn't find user\""))
(invalid-acct-uri ()
"\"Invalid acct URI\"")
(error (any-error)
(format nil "\"Server error: ~A\"" any-error)))))))
(defun server (env user-info-func &optional (clack-app nil)) (defun server (env user-info-func &optional (clack-app nil))