From e6eed6d87522d5dc054d74ffa377c637a42e9e5a Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 9 Jun 2023 16:31:40 +0700 Subject: [PATCH 1/3] chore: return lnurlp status and error message when no data can be retrieved --- main.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 5fcab69..a46834c 100644 --- a/main.go +++ b/main.go @@ -26,10 +26,16 @@ type Config struct { Port int `envconfig:"PORT" default:"3000"` } +type ErrorResponse struct { + Status int + Message string +} + type LNResponse struct { Lnurlp interface{} `json:"lnurlp"` Keysend interface{} `json:"keysend"` Nostr interface{} `json:"nostr"` + Error ErrorResponse `json:"error"` } type GIResponse struct { @@ -110,6 +116,7 @@ func main() { var wg sync.WaitGroup var lnurlp, keysend, nostr interface{} var lnurlpResponse, keysendResponse, nostrResponse *http.Response + var lnurlpError error ln := c.QueryParam("ln") lnurlpUrl, keysendUrl, nostrUrl, err := ToUrl(ln) @@ -120,9 +127,9 @@ func main() { wg.Add(3) go func() { - lnurlp, lnurlpResponse, err = GetJSON(GetJSONParams{url: lnurlpUrl, wg: &wg}) - if err != nil { - e.Logger.Errorf("%v", err) + lnurlp, lnurlpResponse, lnurlpError = GetJSON(GetJSONParams{url: lnurlpUrl, wg: &wg}) + if lnurlpError != nil { + e.Logger.Errorf("%v", lnurlpError) } else { responseBody.Lnurlp = lnurlp } @@ -152,6 +159,10 @@ func main() { if ((lnurlpResponse == nil && keysendResponse == nil && nostrResponse == nil) || (lnurlpResponse.StatusCode >= 300 && keysendResponse.StatusCode >= 300 && nostrResponse.StatusCode >= 300)) { e.Logger.Errorf("Could not retrieve details for lightning address %v", ln) + responseBody.Error = ErrorResponse { + Status: lnurlpResponse.StatusCode, + Message: lnurlpError.Error(), + } return c.JSON(http.StatusBadRequest, &responseBody) } From 5ac07b44235e9e1cf376d6cea822452b79b4d028 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 9 Jun 2023 16:35:40 +0700 Subject: [PATCH 2/3] fix: error json casing --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index a46834c..3162d86 100644 --- a/main.go +++ b/main.go @@ -27,8 +27,8 @@ type Config struct { } type ErrorResponse struct { - Status int - Message string + Status int `json:"status"` + Message string `json:"message"` } type LNResponse struct { From 3d2d953a98a79c02f047f3aabded88675b31199e Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 29 Jun 2023 19:28:52 +0700 Subject: [PATCH 3/3] chore: improve error response to have errors for individual requests --- main.go | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 3162d86..b77e82c 100644 --- a/main.go +++ b/main.go @@ -31,11 +31,17 @@ type ErrorResponse struct { Message string `json:"message"` } +type ErrorsResponse struct { + Lnurlp *ErrorResponse `json:"lnurlp"` + Keysend *ErrorResponse `json:"keysend"` + Nostr *ErrorResponse `json:"nostr"` +} + type LNResponse struct { Lnurlp interface{} `json:"lnurlp"` Keysend interface{} `json:"keysend"` Nostr interface{} `json:"nostr"` - Error ErrorResponse `json:"error"` + Errors ErrorsResponse `json:"errors"` } type GIResponse struct { @@ -116,7 +122,7 @@ func main() { var wg sync.WaitGroup var lnurlp, keysend, nostr interface{} var lnurlpResponse, keysendResponse, nostrResponse *http.Response - var lnurlpError error + var lnurlpError, keysendError, nostrError error ln := c.QueryParam("ln") lnurlpUrl, keysendUrl, nostrUrl, err := ToUrl(ln) @@ -136,18 +142,18 @@ func main() { }() go func() { - keysend, keysendResponse, err = GetJSON(GetJSONParams{url: keysendUrl, wg: &wg}) - if err != nil { - e.Logger.Errorf("%v", err) + keysend, keysendResponse, keysendError = GetJSON(GetJSONParams{url: keysendUrl, wg: &wg}) + if keysendError != nil { + e.Logger.Errorf("%v", keysendError) } else { responseBody.Keysend = keysend } }() go func() { - nostr, nostrResponse, err = GetJSON(GetJSONParams{url: nostrUrl, wg: &wg}) - if err != nil { - e.Logger.Errorf("%v", err) + nostr, nostrResponse, nostrError = GetJSON(GetJSONParams{url: nostrUrl, wg: &wg}) + if nostrError != nil { + e.Logger.Errorf("%v", nostrError) } else { responseBody.Nostr = nostr } @@ -155,14 +161,29 @@ func main() { wg.Wait() - // if the requests resulted in errors return a bad request. something must be wrong with the ln address - if ((lnurlpResponse == nil && keysendResponse == nil && nostrResponse == nil) || - (lnurlpResponse.StatusCode >= 300 && keysendResponse.StatusCode >= 300 && nostrResponse.StatusCode >= 300)) { - e.Logger.Errorf("Could not retrieve details for lightning address %v", ln) - responseBody.Error = ErrorResponse { + responseBody.Errors = ErrorsResponse {} + if (lnurlpError != nil) { + responseBody.Errors.Lnurlp = &ErrorResponse { Status: lnurlpResponse.StatusCode, Message: lnurlpError.Error(), } + } + if (keysendError != nil) { + responseBody.Errors.Keysend = &ErrorResponse { + Status: keysendResponse.StatusCode, + Message: keysendError.Error(), + } + } + if (nostrError != nil) { + responseBody.Errors.Nostr = &ErrorResponse { + Status: nostrResponse.StatusCode, + Message: nostrError.Error(), + } + } + + // if the requests resulted in errors return a bad request. something must be wrong with the ln address + if (lnurlpError != nil && keysendError != nil && nostrError != nil) { + e.Logger.Errorf("Could not retrieve details for lightning address %v", ln) return c.JSON(http.StatusBadRequest, &responseBody) }