Skip to content

Commit ae77491

Browse files
committed
Beginning of recorder
1 parent 60c0500 commit ae77491

File tree

5 files changed

+402
-89
lines changed

5 files changed

+402
-89
lines changed

parrot/errors.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package parrot
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
ErrNilRoute = errors.New("route is nil")
10+
ErrNoMethod = errors.New("no method specified")
11+
ErrInvalidPath = errors.New("invalid path")
12+
ErrNoResponse = errors.New("route must have a handler or some response")
13+
ErrOnlyOneResponse = errors.New("route can only have one response type")
14+
ErrResponseMarshal = errors.New("unable to marshal response body to JSON")
15+
ErrRouteNotFound = errors.New("route not found")
16+
ErrRecorderNotFound = errors.New("recorder not found")
17+
)
18+
19+
// Custom error type to help add more detail to base errors
20+
type dynamicError struct {
21+
Base error // Base error for comparison
22+
Extra string // Dynamic context (e.g., method name)
23+
}
24+
25+
func (e *dynamicError) Error() string {
26+
return fmt.Sprintf("%s: %s", e.Base.Error(), e.Extra)
27+
}
28+
29+
func (e *dynamicError) Unwrap() error {
30+
return e.Base
31+
}
32+
33+
func newDynamicError(base error, detail string) error {
34+
return &dynamicError{
35+
Base: base,
36+
Extra: detail,
37+
}
38+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import (
55
"io"
66
"net/http"
77

8+
"github.com/rs/zerolog"
89
"github.com/smartcontractkit/chainlink-testing-framework/parrot"
910
)
1011

1112
func ExampleServer() {
12-
p, err := parrot.Wake()
13+
// Create a new parrot instance with no logging
14+
p, err := parrot.Wake(parrot.WithLogLevel(zerolog.NoLevel))
1315
if err != nil {
1416
panic(err)
1517
}
1618

19+
// Create a new route /test that will return a 200 status code with a text/plain response body of "Squawk"
1720
route := &parrot.Route{
1821
Method: http.MethodGet,
1922
Path: "/test",
@@ -22,11 +25,13 @@ func ExampleServer() {
2225
ResponseContentType: "text/plain",
2326
}
2427

28+
// Register the route with the parrot instance
2529
err = p.Register(route)
2630
if err != nil {
2731
panic(err)
2832
}
2933

34+
// Call the route
3035
resp, err := p.Call(http.MethodGet, "/test")
3136
if err != nil {
3237
panic(err)
@@ -37,7 +42,6 @@ func ExampleServer() {
3742
fmt.Println(resp.Header.Get("Content-Type"))
3843
body, _ := io.ReadAll(resp.Body)
3944
fmt.Println(string(body))
40-
4145
// Output:
4246
// 200
4347
// text/plain

0 commit comments

Comments
 (0)