diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-01-13 07:07:16 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-01-13 07:07:16 +0000 |
commit | f18e7f6920590ee6f2e59be508518b70a4611638 (patch) | |
tree | 2f80c427e740c2c6f7f2a47be2b869b6d9847e58 /service/http/handler.go | |
parent | 5dc83ef5eee77f4d1ef557e5f8b566e75892680d (diff) | |
parent | bbfcd4fb6eb138c616dab01ea610fbf6ed15985b (diff) |
Merge #472
472: feat(http): Distinct app and internal error codes in the handleError function r=48d90782 a=48d90782
This PR introduces distinct error codes for the app and internal RR errors.
Errors:
```go
roadrunner.ErrNoAssociatedPool
roadrunner.ErrAllocateWorker
roadrunner.ErrWorkerNotReady
roadrunner.ErrEmptyPayload
roadrunner.ErrPoolStopped
roadrunner.ErrWorkerAllocateTimeout
roadrunner.ErrAllWorkersAreDead
```
now associated with the internal error codes. All other errors are application errors.
Some types of errors are impossible to distinguish in the RR1, for example `json.Unmarshall` internal error or similar.
The `.rr.yaml` now contain two more options for the errors.
```yaml
http:
internalErrorCode: 502,
appErrorCode: 502
```
Default behavior unchanged (500 error code as before), but now might be overridden.
closes #471
Co-authored-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'service/http/handler.go')
-rw-r--r-- | service/http/handler.go | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/service/http/handler.go b/service/http/handler.go index eca05483..43f894d7 100644 --- a/service/http/handler.go +++ b/service/http/handler.go @@ -61,11 +61,13 @@ func (e *ResponseEvent) Elapsed() time.Duration { // Handler serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers, // parsed files and query, payload will include parsed form dataTree (if any). type Handler struct { - cfg *Config - log *logrus.Logger - rr *roadrunner.Server - mul sync.Mutex - lsn func(event int, ctx interface{}) + cfg *Config + log *logrus.Logger + rr *roadrunner.Server + mul sync.Mutex + lsn func(event int, ctx interface{}) + internalErrorCode uint64 + appErrorCode uint64 } // Listen attaches handler event controller. @@ -131,6 +133,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // handleError sends error. +/* +handleError distinct RR errors and App errors +You can set return distinct error codes for the App and for the RR +*/ func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error, start time.Time) { // if pipe is broken, there is no sense to write the header // in this case we just report about error @@ -138,8 +144,20 @@ func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error, h.throw(EventError, &ErrorEvent{Request: r, Error: err, start: start, elapsed: time.Since(start)}) return } - // ResponseWriter is ok, write the error code - w.WriteHeader(500) + if errors.Is(err, roadrunner.ErrNoAssociatedPool) || + errors.Is(err, roadrunner.ErrAllocateWorker) || + errors.Is(err, roadrunner.ErrWorkerNotReady) || + errors.Is(err, roadrunner.ErrEmptyPayload) || + errors.Is(err, roadrunner.ErrPoolStopped) || + errors.Is(err, roadrunner.ErrWorkerAllocateTimeout) || + errors.Is(err, roadrunner.ErrAllWorkersAreDead) { + // for the RR errors, write custom error code + w.WriteHeader(int(h.internalErrorCode)) + } else { + // ResponseWriter is ok, write the error code + w.WriteHeader(int(h.appErrorCode)) + } + _, err2 := w.Write([]byte(err.Error())) // error during the writing to the ResponseWriter if err2 != nil { |