golang 웹 프레임 워 크 utron 의 이상 통일 처리
1914 단어 Golang
debug 를 통 해 우 리 는 routes. go 파일 을 찾 았 습 니 다. 안의 wrapController () 방법 은 http 요청 의 입 구 를 처리 하 는 것 입 니 다. 원래 코드 는 다음 과 같 습 니 다.
// wrapController wraps a controller ctrl with method fn, and returns http.HandleFunc
func (r *Router) wrapController(ctx *base.Context, fn string, ctrl controller.Controller) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
r.handleController(ctx, fn, ctrl)
}
}
이곳 의 handle Controller () 방법 은 실현 되 었 습 니 다. 좋 은 처리 전략 이 없 으 며 http Status 500 만 되 돌려 줍 니 다.
// executes the method fn on Controller ctrl, it sets context.
func (r *Router) handleController(ctx *base.Context, fn string, ctrl controller.Controller) {
ctrl.New(ctx)
// execute the method
// TODO: better error handling?
if x := ita.New(ctrl).Call(fn); x.Error() != nil {
ctx.Set(http.StatusInternalServerError)
_, _ = ctx.Write([]byte(x.Error().Error()))
ctx.TextPlain()
_ = ctx.Commit()
return
}
err := ctx.Commit()
if err != nil {
//TODO: Log error
ctx.Log.Errors("=============
%s===========
",err.Error())
}
}
우 리 는 앞 뒤 가 분리 되 어 있 습 니 다. 전단 은 통 일 된 형식의 JSON 을 받 기 를 원 합 니 다. 형식 은 다음 과 같 습 니 다.
4. 567913. 우리 의 방법 도 간단 합 니 다. 바로 wrapController () 에 defer 함 수 를 추가 하여 handle Controller () 에서 던 진 모든 이상 을 캡 처 하 는 것 입 니 다. 코드 는 다음 과 같 습 니 다.
{"Code":"FAIL","Msg":"We caught a panic","Data":"invalid memory address or nil pointer dereference"}
ResultJSon 은 우리 가 정의 한 Struct 로 Code, Msg, Data 세 가지 속성 을 포함 하고 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
golang의 시간대와 신기한 시간.Parse상기 코드의 테스트 결과에서 몇 가지를 얻을 수 있다. time.Now에서 얻은 현재 시간의 시간대는 컴퓨터의 현재 시간대와 같다. time.Parse는 시간 문자열을 Time으로 변환합니다. Time 변수가 어떤 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.