本文整理汇总了Golang中github.com/ursiform/forest.App.Error方法的典型用法代码示例。如果您正苦于以下问题:Golang App.Error方法的具体用法?Golang App.Error怎么用?Golang App.Error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/ursiform/forest.App
的用法示例。
在下文中一共展示了App.Error方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BodyParser
func BodyParser(app *forest.App) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
destination, ok := ctx.Get(forest.Body).(Populater)
if !ok {
ctx.Set(forest.Error,
fmt.Errorf("(*forest.App).BodyParser unitialized"))
message := safeErrorMessage(app, ctx, app.Error("Parse"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
if ctx.Request.Body == nil {
ctx.Set(forest.SafeError,
fmt.Errorf("%s: body is empty", app.Error("Parse")))
message := safeErrorMessage(app, ctx, app.Error("Parse"))
app.Response(ctx, http.StatusBadRequest,
forest.Failure, message).Write(nil)
return
}
if err := destination.Populate(ctx.Request.Body); err != nil {
ctx.Set(forest.SafeError,
fmt.Errorf("%s: %s", app.Error("Parse"), err))
message := safeErrorMessage(app, ctx, app.Error("Parse"))
app.Response(ctx, http.StatusBadRequest,
forest.Failure, message).Write(nil)
return
}
ctx.Next()
}
}
示例2: ErrorsUnauthorized
func ErrorsUnauthorized(app *forest.App) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
app.Response(
ctx,
http.StatusUnauthorized,
forest.Failure,
safeErrorMessage(app, ctx, app.Error("Unauthorized"))).Write(nil)
}
}
示例3: ErrorsServerError
func ErrorsServerError(app *forest.App) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
app.Response(
ctx,
http.StatusInternalServerError,
forest.Failure,
safeErrorMessage(app, ctx, app.Error("Generic"))).Write(nil)
}
}
示例4: ErrorsBadRequest
func ErrorsBadRequest(app *forest.App) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
app.Response(
ctx,
http.StatusBadRequest,
forest.Failure,
safeErrorMessage(app, ctx, app.Error("Generic"))).Write(nil)
}
}
示例5: Authenticate
func Authenticate(app *forest.App) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
userID, ok := ctx.Get(forest.SessionUserID).(string)
if !ok || len(userID) == 0 {
app.Response(ctx, http.StatusUnauthorized, forest.Failure,
app.Error("Unauthorized")).Write(nil)
return
}
ctx.Next()
}
}
示例6: SessionDel
func SessionDel(app *forest.App, manager SessionManager) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
sessionID, ok := ctx.Get(forest.SessionID).(string)
if !ok {
err := fmt.Errorf("SessionDel %s: %v",
forest.SessionID, ctx.Get(forest.SessionID))
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
userID, ok := ctx.Get(forest.SessionUserID).(string)
if !ok {
err := fmt.Errorf("SessionDel %s: %v",
forest.SessionUserID, ctx.Get(forest.SessionUserID))
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
if err := manager.Delete(sessionID, userID); err != nil {
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
ctx.Next()
}
}
示例7: CSRF
func CSRF(app *forest.App) func(ctx *bear.Context) {
type postBody struct {
SessionID string `json:"sessionid"` // forest.SessionID == "sessionid"
}
return func(ctx *bear.Context) {
if ctx.Request.Body == nil {
app.Response(ctx, http.StatusBadRequest,
forest.Failure, app.Error("CSRF")).Write(nil)
return
}
pb := new(postBody)
body, _ := ioutil.ReadAll(ctx.Request.Body)
if body == nil || len(body) < 2 { // smallest JSON body is {}, 2 chars
app.Response(
ctx,
http.StatusBadRequest,
forest.Failure,
app.Error("Parse")).Write(nil)
return
}
// set ctx.Request.Body back to an untouched io.ReadCloser
ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
if err := json.Unmarshal(body, pb); err != nil {
app.Response(
ctx,
http.StatusBadRequest,
forest.Failure,
app.Error("Parse")+": "+err.Error()).Write(nil)
return
}
sessionID, ok := ctx.Get(forest.SessionID).(string)
if !ok || sessionID != pb.SessionID {
app.Response(
ctx,
http.StatusBadRequest,
forest.Failure,
app.Error("CSRF")).Write(nil)
return
}
ctx.Next()
}
}
示例8: ErrorsNotFound
func ErrorsNotFound(app *forest.App) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
message := safeErrorMessage(app, ctx, app.Error("NotFound"))
app.Response(ctx, http.StatusNotFound, forest.Failure, message).Write(nil)
}
}