refactor: ParseError can carry an exit code
Reinstated use of ParseError for all error paths so as to retain existing semantics.
This commit is contained in:
@@ -5,11 +5,17 @@ package kong
|
||||
// It contains the parse Context that triggered the error.
|
||||
type ParseError struct {
|
||||
error
|
||||
Context *Context
|
||||
Context *Context
|
||||
exitCode int
|
||||
}
|
||||
|
||||
// Unwrap returns the original cause of the error.
|
||||
func (p *ParseError) Unwrap() error { return p.error }
|
||||
|
||||
// ExitCode returns the status that Kong should exit with if it fails with a ParseError.
|
||||
func (p *ParseError) ExitCode() int { return exitUsageError }
|
||||
func (p *ParseError) ExitCode() int {
|
||||
if p.exitCode == 0 {
|
||||
return exitNotOk
|
||||
}
|
||||
return p.exitCode
|
||||
}
|
||||
|
||||
@@ -312,34 +312,34 @@ func (k *Kong) extraFlags() []*Flag {
|
||||
func (k *Kong) Parse(args []string) (ctx *Context, err error) {
|
||||
ctx, err = Trace(k, args)
|
||||
if err != nil { // Trace is not expected to return an err
|
||||
return nil, err
|
||||
return nil, &ParseError{error: err, Context: ctx, exitCode: exitUsageError}
|
||||
}
|
||||
if ctx.Error != nil {
|
||||
return nil, &ParseError{error: ctx.Error, Context: ctx}
|
||||
return nil, &ParseError{error: ctx.Error, Context: ctx, exitCode: exitUsageError}
|
||||
}
|
||||
if err = k.applyHook(ctx, "BeforeReset"); err != nil {
|
||||
return nil, err
|
||||
return nil, &ParseError{error: err, Context: ctx}
|
||||
}
|
||||
if err = ctx.Reset(); err != nil {
|
||||
return nil, &ParseError{error: err, Context: ctx}
|
||||
}
|
||||
if err = k.applyHook(ctx, "BeforeResolve"); err != nil {
|
||||
return nil, err
|
||||
return nil, &ParseError{error: err, Context: ctx}
|
||||
}
|
||||
if err = ctx.Resolve(); err != nil {
|
||||
return nil, &ParseError{error: err, Context: ctx}
|
||||
}
|
||||
if err = k.applyHook(ctx, "BeforeApply"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err = ctx.Apply(); err != nil { // Apply is not expected to return an err
|
||||
return nil, err
|
||||
}
|
||||
if err = ctx.Validate(); err != nil {
|
||||
return nil, &ParseError{error: err, Context: ctx}
|
||||
}
|
||||
if _, err = ctx.Apply(); err != nil { // Apply is not expected to return an err
|
||||
return nil, &ParseError{error: err, Context: ctx}
|
||||
}
|
||||
if err = ctx.Validate(); err != nil {
|
||||
return nil, &ParseError{error: err, Context: ctx, exitCode: exitUsageError}
|
||||
}
|
||||
if err = k.applyHook(ctx, "AfterApply"); err != nil {
|
||||
return nil, err
|
||||
return nil, &ParseError{error: err, Context: ctx}
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user