File tree Expand file tree Collapse file tree 4 files changed +44
-3
lines changed Expand file tree Collapse file tree 4 files changed +44
-3
lines changed Original file line number Diff line number Diff line change 4
4
"log"
5
5
"net/http"
6
6
7
+ "github.com/speakeasy-api/speakeasy-api-test-service/internal/errors"
7
8
"github.com/speakeasy-api/speakeasy-api-test-service/internal/pagination"
8
9
"github.com/speakeasy-api/speakeasy-api-test-service/internal/responseHeaders"
9
10
"github.com/speakeasy-api/speakeasy-api-test-service/internal/retries"
@@ -25,6 +26,7 @@ func main() {
25
26
r .HandleFunc ("/pagination/limitoffset/offset" , pagination .HandleLimitOffsetOffset ).Methods (http .MethodGet , http .MethodPut )
26
27
r .HandleFunc ("/pagination/cursor" , pagination .HandleCursor ).Methods (http .MethodGet , http .MethodPut )
27
28
r .HandleFunc ("/retries" , retries .HandleRetries ).Methods (http .MethodGet )
29
+ r .HandleFunc ("/errors/{status_code}" , errors .HandleErrors ).Methods (http .MethodGet )
28
30
29
31
log .Println ("Listening on :8080" )
30
32
if err := http .ListenAndServe (":8080" , r ); err != nil {
Original file line number Diff line number Diff line change
1
+ package errors
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "net/http"
7
+ "strconv"
8
+
9
+ "github.com/gorilla/mux"
10
+ "github.com/speakeasy-api/speakeasy-api-test-service/internal/utils"
11
+
12
+ "github.com/speakeasy-api/speakeasy-api-test-service/pkg/models"
13
+ )
14
+
15
+ func HandleErrors (w http.ResponseWriter , r * http.Request ) {
16
+ vars := mux .Vars (r )
17
+ statusCode , ok := vars ["status_code" ]
18
+ if ! ok {
19
+ utils .HandleError (w , fmt .Errorf ("status_code is required" ))
20
+ return
21
+ }
22
+
23
+ statusCodeInt , err := strconv .Atoi (statusCode )
24
+ if err != nil {
25
+ utils .HandleError (w , fmt .Errorf ("status_code must be an integer" ))
26
+ return
27
+ }
28
+
29
+ w .WriteHeader (statusCodeInt )
30
+
31
+ if err := json .NewEncoder (w ).Encode (models.Error {
32
+ Code : statusCode ,
33
+ Message : "an error occurred" ,
34
+ }); err != nil {
35
+ utils .HandleError (w , err )
36
+ return
37
+ }
38
+ }
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ func HandleError(w http.ResponseWriter, err error) {
15
15
log .Println (err )
16
16
17
17
data , marshalErr := json .Marshal (models.ErrorResponse {
18
- Error : models.ErrorMessage {
18
+ Error : models.Error {
19
19
Message : err .Error (),
20
20
},
21
21
})
Original file line number Diff line number Diff line change 1
1
package models
2
2
3
- type ErrorMessage struct {
3
+ type Error struct {
4
4
Message string `json:"message"`
5
+ Code string `json:"code"`
5
6
}
6
7
7
8
type ErrorResponse struct {
8
- Error ErrorMessage `json:"error"`
9
+ Error Error `json:"error"`
9
10
}
10
11
11
12
type HeaderAuth struct {
You can’t perform that action at this time.
0 commit comments