mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-05 15:18:43 +00:00
Add client error to sdk and fix purge cli
This commit is contained in:
parent
ff2469ec5b
commit
e86b08a724
2 changed files with 37 additions and 6 deletions
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2022 Woodpecker Authors
|
// Copyright 2024 Woodpecker Authors
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
|
@ -16,7 +16,9 @@ package pipeline
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -121,6 +123,11 @@ func pipelinePurge(c *cli.Command, client woodpecker.Client) (err error) {
|
||||||
|
|
||||||
err := client.PipelineDelete(repoID, p.Number)
|
err := client.PipelineDelete(repoID, p.Number)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var clientErr *woodpecker.ClientError
|
||||||
|
if errors.As(err, &clientErr) && clientErr.StatusCode == http.StatusUnprocessableEntity {
|
||||||
|
log.Error().Err(err).Msgf("failed to delete pipeline %d", p.Number)
|
||||||
|
continue
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,15 @@ const (
|
||||||
// pathVersion = "%s/version"
|
// pathVersion = "%s/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ClientError struct {
|
||||||
|
StatusCode int
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ClientError) Error() string {
|
||||||
|
return fmt.Sprintf("client error %d: %s", e.StatusCode, e.Message)
|
||||||
|
}
|
||||||
|
|
||||||
type client struct {
|
type client struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
addr string
|
addr string
|
||||||
|
@ -116,16 +125,25 @@ func (c *client) do(rawURL, method string, in, out any) error {
|
||||||
func (c *client) open(rawURL, method string, in any) (io.ReadCloser, error) {
|
func (c *client) open(rawURL, method string, in any) (io.ReadCloser, error) {
|
||||||
uri, err := url.Parse(rawURL)
|
uri, err := url.Parse(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, &ClientError{
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(method, uri.String(), nil)
|
req, err := http.NewRequest(method, uri.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, &ClientError{
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if in != nil {
|
if in != nil {
|
||||||
decoded, decodeErr := json.Marshal(in)
|
decoded, decodeErr := json.Marshal(in)
|
||||||
if decodeErr != nil {
|
if decodeErr != nil {
|
||||||
return nil, decodeErr
|
return nil, &ClientError{
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Message: decodeErr.Error(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(decoded)
|
buf := bytes.NewBuffer(decoded)
|
||||||
req.Body = io.NopCloser(buf)
|
req.Body = io.NopCloser(buf)
|
||||||
|
@ -135,12 +153,18 @@ func (c *client) open(rawURL, method string, in any) (io.ReadCloser, error) {
|
||||||
}
|
}
|
||||||
resp, err := c.client.Do(req)
|
resp, err := c.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, &ClientError{
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if resp.StatusCode > http.StatusPartialContent {
|
if resp.StatusCode > http.StatusPartialContent {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
out, _ := io.ReadAll(resp.Body)
|
out, _ := io.ReadAll(resp.Body)
|
||||||
return nil, fmt.Errorf("client error %d: %s", resp.StatusCode, string(out))
|
return nil, &ClientError{
|
||||||
|
StatusCode: resp.StatusCode,
|
||||||
|
Message: string(out),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return resp.Body, nil
|
return resp.Body, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue