Skip to content

Commit

Permalink
chore: use error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed May 28, 2024
1 parent 5e0cb52 commit 453a54f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
11 changes: 11 additions & 0 deletions internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,18 @@ type ResponseEvent struct {

type ErrorResponse struct {
Message string `json:"message"`
Error string `json:"error"`
}

type InfoRequest struct {
RelayUrl string `json:"relayUrl"`
WalletPubkey string `json:"walletPubkey"`
}

type InfoResponse struct {
Event *nostr.Event `json:"event"`
}

type NIP47Request struct {
RelayUrl string `json:"relayUrl"`
WalletPubkey string `json:"walletPubkey"`
Expand All @@ -158,6 +163,12 @@ type PublishRequest struct {
SignedEvent *nostr.Event `json:"event"`
}

type PublishResponse struct {
EventId string `json:"eventId"`
RelayUrl string `json:"relayUrl"`
State string `json:"state"`
}

type SubscriptionRequest struct {
RelayUrl string `json:"relayUrl"`
WebhookUrl string `json:"webhookUrl"`
Expand Down
41 changes: 28 additions & 13 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,23 @@ func (svc *Service) InfoHandler(c echo.Context) error {
var requestData InfoRequest
if err := c.Bind(&requestData); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: fmt.Sprintf("error decoding info request: %s", err.Error()),
Message: "Error decoding info request",
Error: err.Error(),
})
}

if (requestData.WalletPubkey == "") {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "wallet pubkey is empty",
Message: "Wallet pubkey is empty",
Error: "no wallet pubkey",
})
}

relay, isCustomRelay, err := svc.getRelayConnection(c.Request().Context(), requestData.RelayUrl)
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: fmt.Sprintf("error connecting to relay: %s", err.Error()),
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Error connecting to relay",
Error: err.Error(),
})
}
if isCustomRelay {
Expand All @@ -187,33 +190,39 @@ func (svc *Service) InfoHandler(c echo.Context) error {
defer cancel()
sub, err := relay.Subscribe(ctx, []nostr.Filter{filter})
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: fmt.Sprintf("error subscribing to relay: %s", err.Error()),
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Error subscribing to relay",
Error: err.Error(),
})
}

select {
case <-ctx.Done():
svc.Logger.Info("exiting subscription.")
return c.JSON(http.StatusRequestTimeout, ErrorResponse{
Message: "request canceled or timed out",
Message: "Request canceled or timed out",
Error: err.Error(),
})
case event := <-sub.Events:
return c.JSON(http.StatusOK, event)
return c.JSON(http.StatusOK, InfoResponse{
Event: event,
})
}
}

func (svc *Service) PublishHandler(c echo.Context) error {
var requestData PublishRequest
if err := c.Bind(&requestData); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: fmt.Sprintf("error decoding publish request: %s", err.Error()),
Message: "Error decoding publish request",
Error: err.Error(),
})
}

if (requestData.SignedEvent == nil) {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "signed event is empty",
Message: "Signed event is empty",
Error: "no signed event",
})
}

Expand All @@ -223,7 +232,8 @@ func (svc *Service) PublishHandler(c echo.Context) error {
relay, isCustomRelay, err := svc.getRelayConnection(ctx, requestData.RelayUrl)
if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "error connecting to relay: " + err.Error(),
Message: "Error connecting to relay",
Error: err.Error(),
})
}

Expand All @@ -244,7 +254,8 @@ func (svc *Service) PublishHandler(c echo.Context) error {
}).Error("Failed to publish event")

return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "error publishing the event: " + err.Error(),
Message: "Error publishing the event",
Error: err.Error(),
})
}

Expand All @@ -253,7 +264,11 @@ func (svc *Service) PublishHandler(c echo.Context) error {
"relayUrl": requestData.RelayUrl,
}).Info("Published event")

return c.JSON(http.StatusOK, "published")
return c.JSON(http.StatusOK, PublishResponse{
EventId: requestData.SignedEvent.ID,
RelayUrl: requestData.RelayUrl,
State: "PUBLISHED",
})
}

func (svc *Service) NIP47Handler(c echo.Context) error {
Expand Down

0 comments on commit 453a54f

Please sign in to comment.