Commit adae7aff authored by zauberstuhl's avatar zauberstuhl

Render json error messages and add catch-all route

parent ee6c3922
......@@ -25,6 +25,8 @@ import (
type ApiAspect struct {
*revel.Controller
ApiHelper
}
func (a ApiAspect) ShowPerson() revel.Result {
......@@ -37,13 +39,13 @@ func (a ApiAspect) ShowPerson() revel.Result {
user, err := models.CurrentUser(a.Params, a.Session)
if err != nil {
revel.AppLog.Error("Failed to get current user :%s", err.Error())
return a.RenderError(err)
return a.RenderApiError(err)
}
err = aspects.FindByUserPersonID(user.ID, personID)
if err != nil {
revel.AppLog.Error("Failed to find user by person id :%s", err.Error())
return a.RenderError(err)
return a.RenderApiError(err)
}
return a.RenderJSON(aspects)
......@@ -63,7 +65,7 @@ func (a ApiAspect) CreatePerson() revel.Result {
err := membership.Create()
if err != nil {
revel.AppLog.Error("Failed to create membership :%s", err.Error())
return a.RenderError(err)
return a.RenderApiError(err)
}
return a.RenderJSON(membership)
......@@ -83,7 +85,7 @@ func (a ApiAspect) DeletePerson() revel.Result {
err := membership.Delete()
if err != nil {
revel.AppLog.Error("Failed to delete membership :%s", err.Error())
return a.RenderError(err)
return a.RenderApiError(err)
}
return a.RenderJSON(membership)
......@@ -93,7 +95,7 @@ func (a ApiAspect) Index() revel.Result {
user, err := models.CurrentUser(a.Params, a.Session)
if err != nil {
revel.AppLog.Error("Failed to get current user :%s", err.Error())
return a.RenderError(err)
return a.RenderApiError(err)
}
return a.RenderJSON(user.Aspects)
......@@ -104,13 +106,13 @@ func (a ApiAspect) Create() revel.Result {
if name == "" {
errMsg := "Aspect name seams to be empty"
revel.AppLog.Error(errMsg)
return a.RenderError(errors.New(errMsg))
return a.RenderApiError(errors.New(errMsg))
}
user, err := models.CurrentUser(a.Params, a.Session)
if err != nil {
revel.AppLog.Error("Failed to get current user :%s", err.Error())
return a.RenderError(err)
return a.RenderApiError(err)
}
aspect := models.Aspect{
......@@ -121,7 +123,7 @@ func (a ApiAspect) Create() revel.Result {
err = aspect.Create()
if err != nil {
revel.AppLog.Error("Failed creating an aspect :%s", err.Error())
return a.RenderError(err)
return a.RenderApiError(err)
}
return a.RenderJSON(aspect)
......
package controllers
//
// GangGo API Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import (
"net/http"
"github.com/revel/revel"
"errors"
)
type ApiHelper struct {
*revel.Controller
}
type ApiError struct {
Error string `json:"error"`
}
func (c ApiHelper) CatchAll() revel.Result {
c.Response.Status = http.StatusNotImplemented
return c.RenderApiError(errors.New("Not implemented"))
}
func (c ApiHelper) RenderApiError(err error) revel.Result {
c.Response.Status = http.StatusBadRequest
return c.RenderJSON(ApiError{err.Error()})
}
......@@ -26,6 +26,8 @@ import (
type ApiComment struct {
*revel.Controller
ApiHelper
}
func (c ApiComment) Index() revel.Result {
......@@ -38,7 +40,7 @@ func (c ApiComment) Index() revel.Result {
err := comments.FindByPostID(postID)
if err != nil {
revel.AppLog.Error(err.Error())
return c.NotFound("Post not found")
return c.RenderApiError(err)
}
return c.RenderJSON(comments)
}
......@@ -56,13 +58,13 @@ func (c ApiComment) Create() revel.Result {
user, err := models.CurrentUser(c.Params, c.Session)
if err != nil {
revel.AppLog.Error(err.Error())
return c.RenderError(err)
return c.RenderApiError(err)
}
err = post.FindByID(postID)
if err != nil {
revel.AppLog.Error(err.Error())
return c.NotFound("Post not found")
return c.RenderApiError(err)
}
dispatcher := jobs.Dispatcher{
......
......@@ -27,6 +27,8 @@ import (
type ApiLike struct {
*revel.Controller
ApiHelper
}
func (l ApiLike) Index() revel.Result {
......@@ -39,7 +41,7 @@ func (l ApiLike) Index() revel.Result {
err := likes.FindByPostID(postID)
if err != nil {
revel.AppLog.Error(err.Error())
return l.NotFound("Post not found")
return l.RenderApiError(err)
}
return l.RenderJSON(likes)
......@@ -59,19 +61,19 @@ func (l ApiLike) Create() revel.Result {
err := post.FindByID(postID)
if err != nil {
revel.AppLog.Error(err.Error())
return l.NotFound("Post not found")
return l.RenderApiError(err)
}
user, err := models.CurrentUser(l.Params, l.Session)
if err != nil {
revel.AppLog.Error(err.Error())
return l.RenderError(err)
return l.RenderApiError(err)
}
guid, err := helpers.Uuid()
if err != nil {
revel.AppLog.Error(err.Error())
return l.RenderError(err)
return l.RenderApiError(err)
}
entityLike := federation.EntityLike{
......@@ -85,7 +87,7 @@ func (l ApiLike) Create() revel.Result {
err = like.Create(&entityLike)
if err != nil {
revel.AppLog.Error(err.Error())
return l.RenderError(err)
return l.RenderApiError(err)
}
// deliver to the network
......@@ -101,7 +103,7 @@ func (l ApiLike) Create() revel.Result {
err = parentUser.FindByID(user.Person.UserID)
if err != nil {
revel.AppLog.Error(err.Error())
return l.RenderError(err)
return l.RenderApiError(err)
}
dispatcher.ParentUser = &parentUser
}
......
......@@ -24,20 +24,22 @@ import (
type ApiNotification struct {
*revel.Controller
ApiHelper
}
func (n ApiNotification) Index() revel.Result {
user, err := models.CurrentUser(n.Params, n.Session)
if err != nil {
revel.AppLog.Error(err.Error())
return n.RenderError(err)
return n.RenderApiError(err)
}
var notify models.Notifications
err = notify.FindUnreadByUserID(user.ID)
if err != nil {
revel.AppLog.Error(err.Error())
return n.RenderError(err)
return n.RenderApiError(err)
}
return n.RenderJSON(notify)
......@@ -55,7 +57,7 @@ func (n ApiNotification) Update(id uint) revel.Result {
user, err := models.CurrentUser(n.Params, n.Session)
if err != nil {
revel.AppLog.Error(err.Error())
return n.RenderError(err)
return n.RenderApiError(err)
}
notify := models.Notification{
......@@ -66,7 +68,7 @@ func (n ApiNotification) Update(id uint) revel.Result {
err = notify.Update()
if err != nil {
revel.AppLog.Error(err.Error())
return n.RenderError(err)
return n.RenderApiError(err)
}
return n.RenderJSON(notify)
......
......@@ -24,6 +24,8 @@ import (
type ApiPeople struct {
*revel.Controller
ApiHelper
}
func (p ApiPeople) Index() revel.Result {
......@@ -44,7 +46,7 @@ func (p ApiPeople) Show() revel.Result {
err := person.FindByID(personID)
if err != nil {
revel.AppLog.Error(err.Error())
return p.NotFound("Person not found")
return p.RenderApiError(err)
}
return p.RenderJSON(person)
......
......@@ -31,6 +31,8 @@ import (
type ApiPost struct {
*revel.Controller
ApiHelper
}
func (p ApiPost) Index() revel.Result {
......@@ -44,13 +46,13 @@ func (p ApiPost) Index() revel.Result {
user, err := models.CurrentUser(p.Params, p.Session)
if err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
err = posts.FindAll(user.ID, offset)
if err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
return p.RenderJSON(posts)
......@@ -70,20 +72,20 @@ func (p ApiPost) Create() revel.Result {
if err == nil && emptyPost {
err = errors.New("Cannot submit an empty post!")
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
user, err := models.CurrentUser(p.Params, p.Session)
if err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
// create post
guid, err := helpers.Uuid()
if err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
entity := federation.EntityStatusMessage{
......@@ -103,7 +105,7 @@ func (p ApiPost) Create() revel.Result {
err = post.Create(&entity, false)
if err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
if entity.Public == false {
......@@ -115,7 +117,7 @@ func (p ApiPost) Create() revel.Result {
ShareableType: models.ShareablePost,
}; if err := shareable.Create(); err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
// create an aspect visibillity entry
......@@ -127,7 +129,7 @@ func (p ApiPost) Create() revel.Result {
}
if err := visibility.Create(); err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
}
......@@ -147,7 +149,7 @@ func (p ApiPost) Show(guid string) revel.Result {
user, err := models.CurrentUser(p.Params, p.Session)
if err != nil {
revel.AppLog.Error(err.Error())
return p.RenderError(err)
return p.RenderApiError(err)
}
postID, err := strconv.ParseUint(guid, 10, 32);
......@@ -158,7 +160,7 @@ func (p ApiPost) Show(guid string) revel.Result {
}
if err != nil {
revel.AppLog.Error(err.Error())
return p.NotFound("Post not found")
return p.RenderApiError(err)
}
return p.RenderJSON(post)
......
......@@ -24,6 +24,8 @@ import (
type ApiProfile struct {
*revel.Controller
ApiHelper
}
func (p ApiProfile) Create() revel.Result {
......@@ -40,7 +42,7 @@ func (p ApiProfile) Show() revel.Result {
err := profile.FindByPersonID(personID)
if err != nil {
revel.AppLog.Error(err.Error())
return p.NotFound("Profile not found")
return p.RenderApiError(err)
}
return p.RenderJSON(profile)
......
......@@ -40,3 +40,6 @@ DELETE /api/v0/aspects/:id ApiAspect.Delete
GET /api/v0/people/:id/aspects ApiAspect.ShowPerson
POST /api/v0/people/:id/aspects/:aspect_id ApiAspect.CreatePerson
DELETE /api/v0/people/:id/aspects/:aspect_id ApiAspect.DeletePerson
# API catch-all-rule will result in "not implemented" error
* /api/v0/*catchall ApiHelper.CatchAll
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment