Skip to content
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

use new response error for client's error encoder #40

Merged
merged 5 commits into from
Jan 19, 2017
Merged
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 command/daemon/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (c *command) Execute(cmd *cobra.Command, args []string) {
serverConfig.ListenAddress = Flags.Server.Listen.Address
serverConfig.Logger = c.logger
serverConfig.RequestFuncs = customServer.RequestFuncs()
serverConfig.Router = customServer.Router()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The router is now configurable which makes it easy to provide seamless migration paths towards microkit. This was rather easy to accomplish at the end. Back then I struggled conceptually.

serverConfig.ServiceName = customServer.ServiceName()

newServer, err = server.New(serverConfig)
Expand Down
3 changes: 3 additions & 0 deletions server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func errorDomain(err error) string {
}

func errorMessage(err error) string {
if err == nil {
return ""
}
switch kitErr := err.(type) {
case kithttp.Error:
switch errgoErr := kitErr.Err.(type) {
Expand Down
86 changes: 86 additions & 0 deletions server/response_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package server

import (
microerror "github.com/giantswarm/microkit/error"
kithttp "github.com/go-kit/kit/transport/http"
)

// ResponseErrorConfig represents the configuration used to create a new
// response error.
type ResponseErrorConfig struct {
// Settings.
Underlying error
}

// DefaultResponseErrorConfig provides a default configuration to create a new
// response error by best effort.
func DefaultResponseErrorConfig() ResponseErrorConfig {
return ResponseErrorConfig{
// Settings.
Underlying: nil,
}
}

// New creates a new configured response error.
func NewResponseError(config ResponseErrorConfig) (ResponseError, error) {
// Settings.
if config.Underlying == nil {
return nil, microerror.MaskAnyf(invalidConfigError, "underlying must not be empty")
}

newResponseError := &responseError{
// Internals.
code: CodeUnknownError,
message: errorMessage(config.Underlying),

// Settings.
underlying: config.Underlying,
}

return newResponseError, nil
}

type responseError struct {
// Internals.
code string
message string

// Settings.
underlying error
}

func (e *responseError) Code() string {
return e.code
}

func (e *responseError) Error() string {
return e.underlying.Error()
}

func (e *responseError) Message() string {
return e.message
}

func (e *responseError) IsEndpoint() bool {
switch u := e.underlying.(type) {
case kithttp.Error:
switch u.Domain {
case kithttp.DomainEncode:
return true
case kithttp.DomainDecode:
return true
case kithttp.DomainDo:
return true
}
}

return false
}

func (e *responseError) SetCode(code string) {
e.code = code
}

func (e *responseError) SetMessage(message string) {
e.message = message
}
Loading