Skip to content

Handle function call errors more gracefully in routes #24120

Closed
@wxiaoguang

Description

@wxiaoguang

Feature Description

Example: help more problems like #24118

Before

	count, err := actions_model.CountRunners(ctx, opts)
	if err != nil {
		ctx.ServerError("AdminRunners", err)
		return
	}

There are some problems:

  1. The function names changes during refactoring, so the ServerError frequently gets out-of-sync.
  2. People have to copy&paste duplicate code again and again.
  3. The return could be forgotten, a lot of times.

Proposal 1

Use generic, let a function like ErrorProof handles server errors automatically.

ErrorProof can use reflect to get the target function's name, it doesn't affect performance because in most cases there is no "internal server error".

	count, err := ctx.ErrorProof(actions_model.CountRunners, ctx, opts))
	if err != nil {
		return
	}

Proposal 2

Return the err directly, make our handler framework support such return value for handler functions (with stacktrace):

	count, err := actions_model.CountRunners(ctx, opts)
	if err != nil {
		return ctx.ErrorWithStack(err)
	}

Feasible?

These are just some early ideas, haven't tried whether it is really feasible, but if people like this proposal, I will spend some time to try.

I really prefer the second one, it looks clear and intuitive

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/featureCompletely new functionality. Can only be merged if feature freeze is not active.type/proposalThe new feature has not been accepted yet but needs to be discussed first.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions