(t *testing.T)
| 347 | } |
| 348 | |
| 349 | func TestFailureReason(t *testing.T) { |
| 350 | apiError := func(status int, message string) error { |
| 351 | u, err := url.Parse("https://api.github.com/repos/monalisa/monas-cafe/issues/142/artifacts/9") |
| 352 | require.NoError(t, err) |
| 353 | return api.HTTPError{HTTPError: &ghAPI.HTTPError{StatusCode: status, Message: message, RequestURL: u}} |
| 354 | } |
| 355 | |
| 356 | tests := []struct { |
| 357 | name string |
| 358 | err error |
| 359 | want string |
| 360 | }{ |
| 361 | { |
| 362 | name: "an API error gives the server's message", |
| 363 | err: apiError(404, "Not Found"), |
| 364 | want: "Not Found", |
| 365 | }, |
| 366 | { |
| 367 | name: "a message on several lines stays on one line", |
| 368 | err: apiError(422, "Validation Failed\nname is invalid"), |
| 369 | want: "Validation Failed name is invalid", |
| 370 | }, |
| 371 | { |
| 372 | name: "an API error without a message gives the whole error", |
| 373 | err: apiError(502, ""), |
| 374 | want: "HTTP 502 (https://api.github.com/repos/monalisa/monas-cafe/issues/142/artifacts/9)", |
| 375 | }, |
| 376 | { |
| 377 | name: "an error that isn't from the API gives its own text", |
| 378 | err: errors.New("dial tcp: lookup api.github.com: no such host"), |
| 379 | want: "dial tcp: lookup api.github.com: no such host", |
| 380 | }, |
| 381 | { |
| 382 | name: "an error that wraps an API error gives its whole text, which names what failed", |
| 383 | err: fmt.Errorf("could not upload latte-art.png: %w", apiError(422, "Validation Failed")), |
| 384 | want: "could not upload latte-art.png: HTTP 422: Validation Failed (https://api.github.com/repos/monalisa/monas-cafe/issues/142/artifacts/9)", |
| 385 | }, |
| 386 | } |
| 387 | |
| 388 | for _, tt := range tests { |
| 389 | t.Run(tt.name, func(t *testing.T) { |
| 390 | assert.Equal(t, tt.want, FailureReason(tt.err)) |
| 391 | }) |
| 392 | } |
| 393 | } |
nothing calls this directly
no test coverage detected