Skip to content

feat: Add errorUnions feature testing endpoint #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func main() {
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet, http.MethodPost)
r.HandleFunc("/retries/after", retries.HandleRetries).Methods(http.MethodGet)
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet, http.MethodPost)
r.HandleFunc("/errors/union/{tag}", errors.HandleErrorsUnion).Methods(http.MethodGet)
r.HandleFunc("/optional", acceptHeaders.HandleAcceptHeaderMultiplexing).Methods(http.MethodGet)
r.HandleFunc("/readonlyorwriteonly", readonlywriteonly.HandleReadOrWrite).Methods(http.MethodPost)
r.HandleFunc("/readonlyandwriteonly", readonlywriteonly.HandleReadAndWrite).Methods(http.MethodPost)
Expand Down
82 changes: 82 additions & 0 deletions internal/errors/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errors

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -55,3 +56,84 @@ func HandleErrors(w http.ResponseWriter, r *http.Request) {
return
}
}

// Returns one of an errors union with a 400 Bad Request status, based on the
// provided tag name in the path parameters with these response schemas:
//
// taggedError1:
// type: object
// properties:
// tag:
// type: string
// enum: [tag1]
// error:
// type: string
// required:
// - tag
// - error
// taggedError2:
// type: object
// properties:
// tag:
// type: string
// const: tag2
// error:
// type: object
// properties:
// message:
// type: string
// required:
// - message
// required:
// - tag
// - error
func HandleErrorsUnion(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
tag, ok := vars["tag"]

if !ok {
utils.HandleError(w, fmt.Errorf("tag path parameter is required"))
return
}

if tag != "tag1" && tag != "tag2" {
utils.HandleError(w, fmt.Errorf("tag path parameter must be either tag1 or tag2"))
return
}

var errorRes any
var responseBody bytes.Buffer

if tag == "tag1" {
errorRes = struct {
Tag string `json:"tag"`
Error string `json:"error"`
}{
Tag: "tag1",
Error: "intentional tag1 error",
}
} else {
errorRes = struct {
Tag string `json:"tag"`
Error struct {
Message string `json:"message"`
} `json:"error"`
}{
Tag: "tag2",
Error: struct {
Message string `json:"message"`
}{
Message: "intentional tag2 error",
},
}
}

if err := json.NewEncoder(&responseBody).Encode(errorRes); err != nil {
utils.HandleError(w, err)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(responseBody.Bytes())
}
Loading