From 8d23867d2c6d3c6f0dd9681192b9b8642ceea254 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Mon, 2 Jan 2023 08:07:21 -0500 Subject: clean up error/response/not found handling --- http/router.go | 74 +++++++++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 48 deletions(-) (limited to 'http/router.go') diff --git a/http/router.go b/http/router.go index 9730dbf..23843c4 100644 --- a/http/router.go +++ b/http/router.go @@ -16,6 +16,18 @@ func NewRouter(registry *registry.Registry) *Router { return &Router{registry: registry} } +func render(c *gin.Context, resource map[string]interface{}, err error) { + if resource == nil && err == nil { + c.JSON(http.StatusNotFound, map[string]interface{}{}) + } else if err != nil { + jsonErr := c.Error(err) + c.JSON(http.StatusInternalServerError, jsonErr.JSON()) + } else { + c.Writer.Header().Set("Content-Type", "application/activity+json") + c.JSON(http.StatusOK, resource) + } +} + func (s *Router) Start(zapWriter io.Writer) { router := gin.Default() router.SetTrustedProxies(nil) @@ -37,36 +49,20 @@ func (s *Router) Start(zapWriter io.Writer) { // "User" endpoint router.GET("/actors/:actor", func(c *gin.Context) { actorParam := c.Param("actor") - resource, _ := s.registry.Profile(actorParam) - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } - + resource, err := s.registry.Profile(actorParam) + render(c, resource, err) }) router.GET("/actors/:actor/followers", func(c *gin.Context) { actorParam := c.Param("actor") - resource, _ := s.registry.Followers(actorParam) - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.Followers(actorParam) + render(c, resource, err) }) router.GET("/actors/:actor/following", func(c *gin.Context) { actorParam := c.Param("actor") - resource, _ := s.registry.Following(actorParam) - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.Following(actorParam) + render(c, resource, err) }) // // Inbox @@ -85,47 +81,29 @@ func (s *Router) Start(zapWriter io.Writer) { router.GET("/actors/:actor/outbox", func(c *gin.Context) { actorParam := c.Param("actor") var resource map[string]interface{} + var err error if c.Query("page") == "true" { - resource, _ = s.registry.OutboxCollection(actorParam) + resource, err = s.registry.OutboxCollection(actorParam) } else { - resource, _ = s.registry.Outbox(actorParam) - } - if resource != nil { - c.JSON(http.StatusOK, resource) - c.Writer.Header().Set("Content-Type", "application/activity+json") - } else { - c.JSON(http.StatusNotFound, nil) + resource, err = s.registry.Outbox(actorParam) } + render(c, resource, err) }) // Single activity router.GET("/actors/:actor/activity/:id", func(c *gin.Context) { actorParam := c.Param("actor") idParam := c.Param("id") - var resource map[string]interface{} - resource, _ = s.registry.ActivityOrNote("activity", actorParam, idParam) - - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.ActivityOrNote("activity", actorParam, idParam) + render(c, resource, err) }) // Single note router.GET("/actors/:actor/activity/:id/note", func(c *gin.Context) { actorParam := c.Param("actor") idParam := c.Param("id") - var resource map[string]interface{} - resource, _ = s.registry.ActivityOrNote("note", actorParam, idParam) - - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.ActivityOrNote("note", actorParam, idParam) + render(c, resource, err) }) router.Run() -- cgit v1.2.3