add descriptive error

This commit is contained in:
Peter Rounce 2022-08-25 21:22:27 +00:00
parent 8d1d686355
commit 44b2f0ba6e
2 changed files with 19 additions and 4 deletions

15
main.go
View file

@ -7,10 +7,17 @@ import (
) )
func write_error(w http.ResponseWriter) { func write_error(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"ERROR","reason":"bad request"}`) jsonData := []byte(`{"status":"ERROR","reason":"bad request"}`)
w.Write(jsonData) w.Write(jsonData)
}
func write_error_message(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"ERROR","reason":"` + message + `"}`)
w.Write(jsonData)
} }
func main() { func main() {

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"database/sql"
"encoding/json" "encoding/json"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http" "net/http"
@ -55,6 +56,13 @@ func new_card_request(w http.ResponseWriter, req *http.Request) {
lnurlw_base := "lnurlw://" + host_domain + "/ln" lnurlw_base := "lnurlw://" + host_domain + "/ln"
c, err := db_get_new_card(a) c, err := db_get_new_card(a)
if err == sql.ErrNoRows {
log.Debug(err)
write_error_message(w, "one time code was used or does not exist")
return
}
if err != nil { if err != nil {
log.Warn(err) log.Warn(err)
write_error(w) write_error(w)