adding error handling and other things

This commit is contained in:
Lukas Bachschwell 2018-11-07 16:27:39 +01:00
parent 1c16b54802
commit 549d91fbb4
7 changed files with 70 additions and 34 deletions

View File

@ -7,6 +7,12 @@
},
"cmd_args": [],
"envs": [],
"dir_structure": {
"watch_all": true,
"controllers": "controllers",
"models": "models",
"others": ["services"]
},
"database": {
"driver": "postgres",
"conn": "postgres://postgres:postgre@127.0.0.1:5435/system?sslmode=disable"

View File

@ -9,7 +9,8 @@ type JsonBasicResponse struct {
Message string
}
const JSON_ERROR int = 500
const JSON_ERROR int = 400
const JSON_INT_ERROR int = 500
const JSON_SUCCESS int = 200
// BaseController operations for BaseController
@ -17,14 +18,21 @@ type BaseController struct {
beego.Controller
}
func (this *BaseController) ServeJsonError(message string) {
func (c *BaseController) ServeJsonError(message string) {
json := JsonBasicResponse{JSON_ERROR, message}
this.Data["json"] = &json
this.ServeJSON()
c.Data["json"] = &json
///c.Ctx.ResponseWriter.WriteHeader(400)
c.ServeJSON()
}
func (this *BaseController) ServeJsonSuccess(message string) {
json := JsonBasicResponse{JSON_SUCCESS, message}
this.Data["json"] = &json
this.ServeJSON()
func (c *BaseController) ServeJsonErrorWithCode(errorcode int, message string) {
json := JsonBasicResponse{errorcode, message}
c.Data["json"] = &json
c.ServeJSON()
}
func (c *BaseController) ServeJsonSuccess(message string) {
json := JsonBasicResponse{JSON_SUCCESS, message}
c.Data["json"] = &json
c.ServeJSON()
}

View File

@ -1,20 +1,13 @@
package controllers
import (
//"fmt"
"github.com/juusechec/jwt-beego"
)
// BaseController operations for APIs
type BaseAPIController struct {
BaseController
}
func (this *BaseAPIController) Prepare() {
if this.Ctx.Input.Method() != "POST" {
this.ServeJsonError("Method not allowed")
}
/*
//Lo que quieras hacer en todos los controladores
// O puede ser leído de una cabecera HEADER!!
tokenString := this.Ctx.Request.Header.Get("X-JWTtoken")
@ -24,13 +17,13 @@ func (this *BaseAPIController) Prepare() {
this.Ctx.Output.SetStatus(401)
this.ServeJsonError("Invalid Token")
}
/*
userSession := this.GetSession("username")
if userSession == nil || userSession != issuer {
this.Ctx.Output.SetStatus(401)
this.ServeJsonError("Invalid Session")
}
return
*/
//return
}

13
controllers/error.go Normal file
View File

@ -0,0 +1,13 @@
package controllers
type ErrorController struct {
BaseController
}
func (c *ErrorController) Error404() {
c.ServeJsonErrorWithCode(404, "Not Found")
}
func (c *ErrorController) Error500() {
c.ServeJsonErrorWithCode(500, "Internal Server Error")
}

14
controllers/index.go Normal file
View File

@ -0,0 +1,14 @@
package controllers
// IndexController operations for Index
type IndexController struct {
BaseController
}
func (c *IndexController) Get() {
c.ServeJsonSuccess("multitenant API")
}
func (c *IndexController) Post() {
c.ServeJsonSuccess("multitenant API")
}

View File

@ -1 +1 @@
{"/Users/LB/go/src/multitenantStack/controllers":1541579833440000000}
{"/Users/LB/go/src/multitenantStack/controllers":1541598684943144901}

View File

@ -46,5 +46,7 @@ func init() {
),
),
)
beego.Router("/", &controllers.IndexController{})
beego.ErrorController(&controllers.ErrorController{})
beego.AddNamespace(ns)
}