Skip to content

Commit

Permalink
chore(*): expose several functions for external use (#409)
Browse files Browse the repository at this point in the history
Co-authored-by: iawia002 <[email protected]>
  • Loading branch information
caicloud-bot and iawia002 authored Dec 8, 2020
1 parent f2e3b26 commit 35b52e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
10 changes: 10 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ func (e *err) Error() string {
return e.message.Message
}

// Reason returns the raw Reason string.
func (e *err) Reason() string {
return string(e.message.Reason)
}

// Data returns the detailed error data.
func (e *err) Data() map[string]string {
return e.message.Data
}

// factory is an error factory.
type factory struct {
code int
Expand Down
13 changes: 7 additions & 6 deletions service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func WriteError(ctx context.Context, producers []Producer, err interface{}) erro
return e
}
if len(producers) <= 0 {
return noProducerToWrite.Error(ats)
return NoProducerToWrite.Error(ats)
}
code := http.StatusInternalServerError
var msg interface{}
Expand All @@ -180,7 +180,7 @@ func WriteError(ctx context.Context, producers []Producer, err interface{}) erro
msg = err
}

producer := chooseProducer(ats, producers)
producer := ChooseProducer(ats, producers)
if producer == nil {
// Choose the first producer
producer = producers[0]
Expand All @@ -203,11 +203,11 @@ func WriteData(ctx context.Context, producers []Producer, code int, data interfa
return err
}
if len(producers) <= 0 {
return noProducerToWrite.Error(ats)
return NoProducerToWrite.Error(ats)
}
producer := chooseProducer(ats, producers)
producer := ChooseProducer(ats, producers)
if producer == nil {
return noProducerToWrite.Error(ats)
return NoProducerToWrite.Error(ats)
}
resp := httpCtx.ResponseWriter()
if resp.HeaderWritable() {
Expand All @@ -221,7 +221,8 @@ func WriteData(ctx context.Context, producers []Producer, code int, data interfa
return producer.Produce(resp, data)
}

func chooseProducer(acceptTypes []string, producers []Producer) Producer {
// ChooseProducer chooses the right producer.
func ChooseProducer(acceptTypes []string, producers []Producer) Producer {
if len(acceptTypes) <= 0 || len(producers) <= 0 {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ var (
NoContext = errors.InternalServerError.Build("Nirvana:Service:NoContext", "can't find http context, you should define `ctx context.Context` as the first parameter of your handler function")
// NoParameterGenerator represents no parameter generator error.
NoParameterGenerator = errors.InternalServerError.Build("Nirvana:Service:NoParameterGenerator", "no parameter generator for source ${source}")
// NoProducerToWrite represents no producer to write error.
NoProducerToWrite = errors.NotAcceptable.Build("Nirvana:Service:noProducerToWrite", "can't find producer for accept types ${types}")
)

var (
Expand All @@ -112,7 +114,6 @@ var (
invalidProducer = errors.InternalServerError.Build("Nirvana:Service:invalidProducer", "${type} is invalid for producer")
noConnectionHijacker = errors.InternalServerError.Build("Nirvana:Service:noConnectionHijacker", "underlying http.ResponseWriter does not implement http.Hijacker")
invalidMetaType = errors.InternalServerError.Build("Nirvana:Service:invalidMetaType", "can't recognize meta for type ${type}")
noProducerToWrite = errors.NotAcceptable.Build("Nirvana:Service:noProducerToWrite", "can't find producer for accept types ${types}")
invalidMethod = errors.InternalServerError.Build("Nirvana:Service:invalidMethod", "http method ${method} is invalid")
invalidStatusCode = errors.InternalServerError.Build("Nirvana:Service:invalidStatusCode", "http status code must be in [100,599]")
invalidBodyType = errors.InternalServerError.Build("Nirvana:Service:invalidBodyType", "${type} is not a valid type for body")
Expand Down

0 comments on commit 35b52e9

Please sign in to comment.