Skip to content

Commit e294017

Browse files
committed
chore: move code to a separate file
1 parent 5695f35 commit e294017

2 files changed

Lines changed: 63 additions & 56 deletions

File tree

internal/revproxy/error_handler.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package revproxy
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/labstack/echo/v4"
10+
)
11+
12+
// The same error content as the data services API
13+
const gwBaseErrorCode int = 6000
14+
15+
type errorContent struct {
16+
Code int `json:"code"`
17+
Message string `json:"message"`
18+
Detail string `json:"detail,omitempty"`
19+
TraceId string `json:"trace_id,omitempty"`
20+
}
21+
22+
type errorResponse struct {
23+
Error errorContent `json:"error"`
24+
}
25+
26+
// Adapted from https://echo.labstack.com/docs/error-handling
27+
func ErrorHandler(err error, c echo.Context) {
28+
if c.Response() != nil && c.Response().Committed {
29+
return // response has been already sent to the client by handler or some middleware
30+
}
31+
32+
accept := c.Request().Header.Get("Accept")
33+
isHTML := strings.Contains(accept, echo.MIMETextHTML)
34+
35+
// If the accept header is html then we fall back to the default handler (for now).
36+
// If the acceptt header is not html or is blank we return json
37+
if isHTML {
38+
c.Echo().DefaultHTTPErrorHandler(err, c)
39+
return
40+
}
41+
42+
code := http.StatusInternalServerError
43+
message := err.Error()
44+
var he *echo.HTTPError
45+
if errors.As(err, &he) { // find error in an error chain that implements HTTPError
46+
if tmp := he.Code; tmp != 0 {
47+
code = tmp
48+
}
49+
if msg := fmt.Sprintf("%v", he.Message); len(msg) > 0 {
50+
message = msg
51+
}
52+
}
53+
54+
var cErr error
55+
if c.Request().Method == http.MethodHead {
56+
cErr = c.NoContent(code)
57+
} else {
58+
cErr = c.JSON(code, errorResponse{Error: errorContent{Code: gwBaseErrorCode + code, Message: message}})
59+
}
60+
if cErr != nil {
61+
c.Logger().Error("failed to send error page to client", "error", errors.Join(err, cErr))
62+
}
63+
}

internal/revproxy/main.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
package revproxy
44

55
import (
6-
"errors"
76
"fmt"
8-
"net/http"
97
"path"
10-
"strings"
118

129
"github.com/SwissDataScienceCenter/renku-gateway/internal/config"
1310
"github.com/SwissDataScienceCenter/renku-gateway/internal/models"
@@ -154,56 +151,3 @@ func NewServer(options ...RevproxyOption) (*Revproxy, error) {
154151
}
155152
return &server, nil
156153
}
157-
158-
// The same error content as the data services API
159-
const gwBaseErrorCode int = 6000
160-
161-
type errorContent struct {
162-
Code int `json:"code"`
163-
Message string `json:"message"`
164-
Detail string `json:"detail,omitempty"`
165-
TraceId string `json:"trace_id,omitempty"`
166-
}
167-
168-
type errorResponse struct {
169-
Error errorContent `json:"error"`
170-
}
171-
172-
// Adapted from https://echo.labstack.com/docs/error-handling
173-
func ErrorHandler(err error, c echo.Context) {
174-
if c.Response() != nil && c.Response().Committed {
175-
return // response has been already sent to the client by handler or some middleware
176-
}
177-
178-
accept := c.Request().Header.Get("Accept")
179-
isHTML := strings.Contains(accept, echo.MIMETextHTML)
180-
181-
// If the accept header is html then we fall back to the default handler (for now).
182-
// If the acceptt header is not html or is blank we return json
183-
if isHTML {
184-
c.Echo().DefaultHTTPErrorHandler(err, c)
185-
return
186-
}
187-
188-
code := http.StatusInternalServerError
189-
message := err.Error()
190-
var he *echo.HTTPError
191-
if errors.As(err, &he) { // find error in an error chain that implements HTTPError
192-
if tmp := he.Code; tmp != 0 {
193-
code = tmp
194-
}
195-
if msg := fmt.Sprintf("%v", he.Message); len(msg) > 0 {
196-
message = msg
197-
}
198-
}
199-
200-
var cErr error
201-
if c.Request().Method == http.MethodHead {
202-
cErr = c.NoContent(code)
203-
} else {
204-
cErr = c.JSON(code, errorResponse{Error: errorContent{Code: gwBaseErrorCode + code, Message: message}})
205-
}
206-
if cErr != nil {
207-
c.Logger().Error("failed to send error page to client", "error", errors.Join(err, cErr))
208-
}
209-
}

0 commit comments

Comments
 (0)