caicloud/nirvana

how can i get execute error?

Closed this issue · 6 comments

Is this a BUG REPORT or FEATURE REQUEST?:

Uncomment only one, leave it on its own line:

/kind bug

/kind feature

What happened:

func (s *service) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	for _, f := range s.filters {
		if !f(resp, req) {
			return
		}
	}
	ctx := newHTTPContext(resp, req)

	executor, err := s.root.Match(ctx, &ctx.container, req.URL.Path)
	if err != nil {
		if err := writeError(ctx, s.producers, err); err != nil {
			s.logger.Error(err)
		}
		return
	}
	err = executor.Execute(ctx)
	if err == nil && ctx.response.HeaderWritable() {
		err = invalidService.Error()
	}
	if err != nil {
		if ctx.response.HeaderWritable() {
			if err := writeError(ctx, s.producers, err); err != nil {
				s.logger.Error(err)
			}
		} else {
			s.logger.Error(err)
		}
	}
}

err = executor.Execute(ctx)can we set this err into context, then we get this err in my middleware. because i want process my common error in last middleware.

example

origin:

Function: func(ctx context.Context, partition string) (*project.ProjectList, error) {
		return project.List(partition)
	}

now : because i need convert apiserver error to nirvana

Function: func(ctx context.Context, partition string) (*project.ProjectList, error) {
		pList, err := project.List(partition)
		if err != nil {
			return nil, errors.APIServerErrorToNirvanaError(err)
		}
		return pList, 
	}

/cc @kdada

What you expected to happen:

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

/assign @kdada

kdada commented

You have misunderstood the workflow of the executor.

Middlewares can't handle the results of your API methods. The error of executor.Execute(ctx) only occurs when framework is in trouble (e.g. can't parse parameters to API method or can't write results of API method to response) or middlewares return errors.

If you want to convert errors from API methods, please use Definition.Results. It contains Operator to convert result types.

got it,thanks

will attemp use Operator convert my API handle result.

Do we have an example to use Operator in Definition.Results?

kdada commented
kdada commented

/close