Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: return lnurlp status and error message when no data can be retrieved #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ type Config struct {
Port int `envconfig:"PORT" default:"3000"`
}

type ErrorResponse struct {
Status int `json:"status"`
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"`
Errors ErrorsResponse `json:"errors"`
}

type GIResponse struct {
Expand Down Expand Up @@ -110,6 +122,7 @@ func main() {
var wg sync.WaitGroup
var lnurlp, keysend, nostr interface{}
var lnurlpResponse, keysendResponse, nostrResponse *http.Response
var lnurlpError, keysendError, nostrError error

ln := c.QueryParam("ln")
lnurlpUrl, keysendUrl, nostrUrl, err := ToUrl(ln)
Expand All @@ -120,37 +133,56 @@ 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
}
}()

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
}
}()

wg.Wait()

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 ((lnurlpResponse == nil && keysendResponse == nil && nostrResponse == nil) ||
(lnurlpResponse.StatusCode >= 300 && keysendResponse.StatusCode >= 300 && nostrResponse.StatusCode >= 300)) {
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)
}
Expand Down