aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulio Capote <jcapote@gmail.com>2023-01-02 13:07:21 +0000
committerJulio Capote <jcapote@gmail.com>2023-01-02 13:07:21 +0000
commit8d23867d2c6d3c6f0dd9681192b9b8642ceea254 (patch)
treebb2a1609ebd188d2902fe9cf7d2590527e17c55b
parent81873a1c6786b834a50486137f7b92b60e901d28 (diff)
downloadcommunique-8d23867d2c6d3c6f0dd9681192b9b8642ceea254.tar.gz
clean up error/response/not found handling
-rw-r--r--http/router.go74
-rw-r--r--models/persister.go8
-rw-r--r--registry/registry.go3
3 files changed, 37 insertions, 48 deletions
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()
diff --git a/models/persister.go b/models/persister.go
index 4381008..c6e0c72 100644
--- a/models/persister.go
+++ b/models/persister.go
@@ -82,14 +82,22 @@ func (p *Persister) Collect(model model) ([][]byte, error) {
func (p *Persister) Find(model model) ([]byte, error) {
var result []byte
var item *badger.Item
+ var notFound bool
err := p.db.View(func(txn *badger.Txn) error {
var getErr error
item, getErr = txn.Get([]byte(model.Key()))
+ if getErr == badger.ErrKeyNotFound {
+ notFound = true
+ return nil
+ }
if getErr != nil {
return getErr
}
return nil
})
+ if notFound {
+ return nil, nil // on key not found we want to return nil, so that gin returns 404 and not 500
+ }
if err != nil {
return nil, err
}
diff --git a/registry/registry.go b/registry/registry.go
index 0a5df77..21f4622 100644
--- a/registry/registry.go
+++ b/registry/registry.go
@@ -132,6 +132,9 @@ func (r *Registry) ActivityOrNote(activityOrNote, name, id string) (map[string]i
if err != nil {
return nil, err
}
+ if result == nil {
+ return nil, nil
+ }
buf := bytes.NewBuffer(result)
dec := gob.NewDecoder(buf)
var outboxItem models.OutboxItem