16561: Handle implicit port number in ws:// and wss:// urls.
[arvados.git] / lib / service / error.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package service
6
7 import (
8         "context"
9         "net/http"
10
11         "git.arvados.org/arvados.git/sdk/go/arvados"
12         "git.arvados.org/arvados.git/sdk/go/ctxlog"
13         "github.com/sirupsen/logrus"
14 )
15
16 // ErrorHandler returns a Handler that reports itself as unhealthy and
17 // responds 500 to all requests.  ErrorHandler itself logs the given
18 // error once, and the handler logs it again for each incoming
19 // request.
20 func ErrorHandler(ctx context.Context, _ *arvados.Cluster, err error) Handler {
21         logger := ctxlog.FromContext(ctx)
22         logger.WithError(err).Error("unhealthy service")
23         return errorHandler{err, logger}
24 }
25
26 type errorHandler struct {
27         err    error
28         logger logrus.FieldLogger
29 }
30
31 func (eh errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
32         eh.logger.WithError(eh.err).Error("unhealthy service")
33         http.Error(w, "", http.StatusInternalServerError)
34 }
35
36 func (eh errorHandler) CheckHealth() error {
37         return eh.err
38 }
39
40 // Done returns a closed channel to indicate the service has
41 // stopped/failed.
42 func (eh errorHandler) Done() <-chan struct{} {
43         return doneChannel
44 }
45
46 var doneChannel = func() <-chan struct{} {
47         done := make(chan struct{})
48         close(done)
49         return done
50 }()