this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

errors: make Errors and Print respect wrapped errors

Also fix `errors.Promote` so that it will behave correctly when its
argument contains a `%` character.

Change-Id: I95d90c07d2389e038abb4769b0e99a07b3a03bbe
Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/529383
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>

+67 -31
+30 -19
cue/errors/errors.go
··· 254 254 case Error: 255 255 return x 256 256 default: 257 - return Wrapf(err, token.NoPos, msg) 257 + return Wrapf(err, token.NoPos, "%s", msg) 258 258 } 259 259 } 260 260 ··· 293 293 // its individual elements. If the given error is not an Error, it will be 294 294 // promoted to one. 295 295 func Errors(err error) []Error { 296 - switch x := err.(type) { 297 - case nil: 296 + if err == nil { 298 297 return nil 299 - case list: 300 - return []Error(x) 301 - case Error: 302 - return []Error{x} 298 + } 299 + var listErr list 300 + var errorErr Error 301 + switch { 302 + case As(err, &listErr): 303 + return listErr 304 + case As(err, &errorErr): 305 + return []Error{errorErr} 303 306 default: 304 307 return []Error{Promote(err, "")} 305 308 } ··· 420 423 // Sanitize sorts multiple errors and removes duplicates on a best effort basis. 421 424 // If err represents a single or no error, it returns the error as is. 422 425 func Sanitize(err Error) Error { 423 - if l, ok := err.(list); ok && err != nil { 424 - a := make(list, len(l)) 425 - copy(a, l) 426 - a.Sort() 427 - a.RemoveMultiples() 428 - return a 426 + if err == nil { 427 + return nil 428 + } 429 + if l, ok := err.(list); ok { 430 + return l.sanitize() 429 431 } 430 432 return err 433 + } 434 + 435 + func (p list) sanitize() list { 436 + if p == nil { 437 + return p 438 + } 439 + a := make(list, len(p)) 440 + copy(a, p) 441 + a.Sort() 442 + a.RemoveMultiples() 443 + return a 431 444 } 432 445 433 446 // Sort sorts an List. *posError entries are sorted by position, ··· 534 547 if cfg == nil { 535 548 cfg = &Config{} 536 549 } 537 - if e, ok := err.(Error); ok { 538 - err = Sanitize(e) 539 - } 540 - for _, e := range Errors(err) { 550 + for _, e := range list(Errors(err)).sanitize() { 541 551 printError(w, e, cfg) 542 552 } 543 553 } ··· 568 578 569 579 printed := false 570 580 msg, args := err.Msg() 571 - if msg != "" || u == nil { // print at least something 572 - fmt.Fprintf(w, msg, args...) 581 + s := fmt.Sprintf(msg, args...) 582 + if s != "" || u == nil { // print at least something 583 + _, _ = io.WriteString(w, s) 573 584 printed = true 574 585 } 575 586
+37 -12
cue/errors/errors_test.go
··· 16 16 17 17 import ( 18 18 "bytes" 19 + "fmt" 19 20 "testing" 20 21 21 22 "cuelang.org/go/cue/token" ··· 172 173 } 173 174 174 175 func TestPrintError(t *testing.T) { 175 - type args struct { 176 - err error 177 - } 178 176 tests := []struct { 179 177 name string 180 - args args 178 + err error 181 179 wantW string 182 - }{ 183 - // TODO: Add test cases. 184 - } 180 + }{{ 181 + name: "SimplePromoted", 182 + err: Promote(fmt.Errorf("hello"), "msg"), 183 + wantW: "msg: hello\n", 184 + }, { 185 + name: "PromoteWithPercent", 186 + err: Promote(fmt.Errorf("hello"), "msg%s"), 187 + wantW: "msg%s: hello\n", 188 + }, { 189 + name: "PromoteWithEmptyString", 190 + err: Promote(fmt.Errorf("hello"), ""), 191 + wantW: "hello\n", 192 + }, { 193 + name: "TwoErrors", 194 + err: Append(Promote(fmt.Errorf("hello"), "x"), Promote(fmt.Errorf("goodbye"), "y")), 195 + wantW: "x: hello\ny: goodbye\n", 196 + }, { 197 + name: "WrappedSingle", 198 + err: fmt.Errorf("wrap: %w", Promote(fmt.Errorf("hello"), "x")), 199 + wantW: "x: hello\n", 200 + }, { 201 + name: "WrappedMultiple", 202 + err: fmt.Errorf("wrap: %w", 203 + Append(Promote(fmt.Errorf("hello"), "x"), Promote(fmt.Errorf("goodbye"), "y")), 204 + ), 205 + wantW: "x: hello\ny: goodbye\n", 206 + }} 207 + // TODO tests for errors with positions. 185 208 for _, tt := range tests { 186 - w := &bytes.Buffer{} 187 - Print(w, tt.args.err, nil) 188 - if gotW := w.String(); gotW != tt.wantW { 189 - t.Errorf("%q. PrintError() = %v, want %v", tt.name, gotW, tt.wantW) 190 - } 209 + t.Run(tt.name, func(t *testing.T) { 210 + w := &bytes.Buffer{} 211 + Print(w, tt.err, nil) 212 + if gotW := w.String(); gotW != tt.wantW { 213 + t.Errorf("unexpected PrintError result\ngot %q\nwant %q", gotW, tt.wantW) 214 + } 215 + }) 191 216 } 192 217 }