(err error, statusCode int, enMessage string, zhMessage string, canTryStale bool)
| 66 | } |
| 67 | |
| 68 | func (this *HTTPRequest) write50x(err error, statusCode int, enMessage string, zhMessage string, canTryStale bool) { |
| 69 | if err != nil { |
| 70 | this.addError(err) |
| 71 | } |
| 72 | |
| 73 | // 尝试从缓存中恢复 |
| 74 | if canTryStale && |
| 75 | this.cacheCanTryStale && |
| 76 | this.web.Cache.Stale != nil && |
| 77 | this.web.Cache.Stale.IsOn && |
| 78 | (len(this.web.Cache.Stale.Status) == 0 || lists.ContainsInt(this.web.Cache.Stale.Status, statusCode)) { |
| 79 | var ok = this.doCacheRead(true) |
| 80 | if ok { |
| 81 | return |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // 显示自定义页面 |
| 86 | if this.doPage(statusCode) { |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | // 内置HTML模板 |
| 91 | var pageContent = configutils.ParseVariables(httpStatusPageTemplate, func(varName string) (value string) { |
| 92 | switch varName { |
| 93 | case "status": |
| 94 | return types.String(statusCode) |
| 95 | case "statusMessage": |
| 96 | return http.StatusText(statusCode) |
| 97 | case "requestId": |
| 98 | return this.requestId |
| 99 | case "message": |
| 100 | var acceptLanguages = this.RawReq.Header.Get("Accept-Language") |
| 101 | if len(acceptLanguages) > 0 { |
| 102 | var index = strings.Index(acceptLanguages, ",") |
| 103 | if index > 0 { |
| 104 | var firstLanguage = acceptLanguages[:index] |
| 105 | if firstLanguage == "zh-CN" { |
| 106 | return "网站出了一点小问题,原因:" + zhMessage + "。" |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return "The site is unavailable now, cause: " + enMessage + "." |
| 111 | } |
| 112 | return this.Format("${" + varName + "}") |
| 113 | }) |
| 114 | |
| 115 | this.ProcessResponseHeaders(this.writer.Header(), statusCode) |
| 116 | this.writer.WriteHeader(statusCode) |
| 117 | |
| 118 | _, _ = this.writer.Write([]byte(pageContent)) |
| 119 | } |
no test coverage detected