Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit 813bcea

Browse files
authored
Add common packages for http and tracing (#10)
**What** - Added common errors - Added `BaseHandler` - Added parameter resolution and normalization - Added profiling endpoint - Added error models - Deleted obsolete tracing utils - Added `Tracer` - Fix linting issues and update linter - Fix all linter issues and fix `fileutils` tests - Disabled GitHub actions
1 parent 93d323a commit 813bcea

33 files changed

+826
-291
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- run:
1414
name: Install go tools
1515
command: |
16-
go get golang.org/x/lint/golint
16+
make setup-env
1717
- run:
1818
name: Print env
1919
command: make env

.github/actions/golint/Dockerfile

Lines changed: 0 additions & 14 deletions
This file was deleted.

.github/workflows/go.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
vendor
1+
bin
2+
vendor
3+
*~

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ help:
2222
env: ## Print debug information about your local environment
2323
@echo git: $(shell git version)
2424
@echo go: $(shell go version)
25-
@echo golint: $(shell which golint)
25+
@echo golangci-lint: $(shell which golangci-lint)
2626
@echo gofmt: $(shell which gofmt)
2727
@echo staticcheck: $(shell which staticcheck)
2828

2929
.PHONY: setup-env
3030
setup-env:
3131
$(shell go mod download)
32-
$(shell go install golang.org/x/lint/golint)
32+
$(shell curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.20.0)
3333

3434
.PHONY: changelog
3535
changelog: ## Print git hitstory based changelog
@@ -58,9 +58,9 @@ test: ## Runs the go tests
5858
@go test -cover ./...
5959

6060
.PHONY: lint
61-
lint: ## Verifies `golint` passes
61+
lint: setup-env ## Verifies `golangci-lint` passes
6262
@echo "+ $@"
63-
@golint -set_exit_status ./...
63+
@./bin/golangci-lint run ./...
6464

6565

6666

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require (
44
github.com/bakins/net-http-recover v0.0.0-20141007104922-6cba69d01459
55
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
66
github.com/go-chi/chi v4.0.2+incompatible
7+
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
78
github.com/golang/protobuf v1.3.2
89
github.com/google/uuid v1.1.1
910
github.com/gorilla/websocket v1.4.0
@@ -13,7 +14,7 @@ require (
1314
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1415
github.com/modern-go/reflect2 v1.0.1 // indirect
1516
github.com/opentracing/opentracing-go v1.1.0
16-
github.com/pkg/errors v0.8.1 // indirect
17+
github.com/pkg/errors v0.8.1
1718
github.com/prometheus/client_golang v1.1.0
1819
github.com/rs/cors v1.7.0
1920
github.com/satori/go.uuid v1.2.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxm
1919
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
2020
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
2121
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
22+
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
23+
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
2224
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
2325
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
2426
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=

pkg/errors/error_type.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package errors
2+
3+
// ErrorType : The type of the error response
4+
type ErrorType string
5+
6+
const (
7+
// GeneralErrorType is a generic error type
8+
GeneralErrorType ErrorType = "GeneralError"
9+
// FieldErrorType is a field validation error type
10+
FieldErrorType ErrorType = "FieldError"
11+
)

pkg/errors/errors.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package errors
2+
3+
import (
4+
"errors"
5+
6+
validation "github.com/go-ozzo/ozzo-validation"
7+
)
8+
9+
var (
10+
// ErrPermission is the standard "Permission Denied" error
11+
ErrPermission = errors.New("You don't have required permission to perform this action")
12+
// ErrAuthorization is the standard "Unauthorized" error
13+
ErrAuthorization = errors.New("User is unauthorized, make sure you've logged in")
14+
// ErrInternal is the standard "Internal Server" error
15+
ErrInternal = errors.New("Internal server error, please try again later")
16+
// ErrInvalidParameters is the standard "Bad Request" error
17+
ErrInvalidParameters = errors.New("Some of the request parameters are not correct")
18+
// ErrUnmarshalling is the JSON deserialization error
19+
ErrUnmarshalling = errors.New("Failed to read JSON from the request body")
20+
// ErrForm is the form parsing error
21+
ErrForm = errors.New("Failed to parse the submitted form")
22+
// ErrNotFound is the standard entiry not found error
23+
ErrNotFound = errors.New("The requested object was not found")
24+
// ErrNotImplemented is intended to be used when stubbing new endpoints
25+
ErrNotImplemented = errors.New("Method is not implemented")
26+
)
27+
28+
// ValidationErrors contains errors organized by validated fields
29+
// for now it's just an alias to the validation library we use
30+
type ValidationErrors = validation.Errors
31+
32+
// ValidationErrorsToFieldErrorResponse converts validation errors to the format that is
33+
// served by HTTP handlers
34+
func ValidationErrorsToFieldErrorResponse(errs ValidationErrors) (fieldErrResp FieldErrorResponse) {
35+
fieldErrResp.Errors = make([]FieldError, 0, len(errs))
36+
for key, fieldErr := range errs {
37+
if fieldErr == nil {
38+
continue
39+
}
40+
fieldErrResp.Errors = append(fieldErrResp.Errors, FieldError{
41+
GeneralError: GeneralError{
42+
Type: FieldErrorType,
43+
Message: fieldErr.Error(),
44+
},
45+
Key: key,
46+
})
47+
}
48+
return fieldErrResp
49+
}

pkg/errors/errors_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package errors
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestValidationErrorsToFieldErrorResponse(t *testing.T) {
11+
cases := []struct {
12+
name string
13+
errs ValidationErrors
14+
expected FieldErrorResponse
15+
}{
16+
{
17+
name: "Returns empty response when errors are nil",
18+
expected: FieldErrorResponse{Errors: []FieldError{}},
19+
},
20+
{
21+
name: "Returns empty response when errors are empty",
22+
errs: ValidationErrors{},
23+
expected: FieldErrorResponse{Errors: []FieldError{}},
24+
},
25+
{
26+
name: "Returns errors in the response when errors are not empty",
27+
errs: ValidationErrors{
28+
"field1": errors.New("bad field1"),
29+
"field2": errors.New("bad field2"),
30+
},
31+
expected: FieldErrorResponse{
32+
Errors: []FieldError{
33+
FieldError{
34+
GeneralError: GeneralError{
35+
Type: FieldErrorType,
36+
Message: "bad field1",
37+
},
38+
Key: "field1",
39+
},
40+
FieldError{
41+
GeneralError: GeneralError{
42+
Type: FieldErrorType,
43+
Message: "bad field2",
44+
},
45+
Key: "field2",
46+
},
47+
},
48+
},
49+
},
50+
{
51+
name: "Does not include nil errors in the response",
52+
errs: ValidationErrors{
53+
"field1": errors.New("bad field1"),
54+
"field2": nil,
55+
},
56+
expected: FieldErrorResponse{
57+
Errors: []FieldError{
58+
FieldError{
59+
GeneralError: GeneralError{
60+
Type: FieldErrorType,
61+
Message: "bad field1",
62+
},
63+
Key: "field1",
64+
},
65+
},
66+
},
67+
},
68+
}
69+
70+
for _, tc := range cases {
71+
t.Run(tc.name, func(t *testing.T) {
72+
require.Equal(t, tc.expected, ValidationErrorsToFieldErrorResponse(tc.errs))
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)