Skip to content

Commit

Permalink
BMA-5998 Display JSON request encoding errors similar to response dec…
Browse files Browse the repository at this point in the history
…oding ones
  • Loading branch information
tardypad committed Nov 15, 2022
1 parent 26985b1 commit a6dc796
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions leaseweb/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ func NewDecodingError(ctx string, err error) *DecodingError {
return &DecodingError{Context: ctx, Message: err.Error()}
}

// EncodingError -
type EncodingError struct {
Context string
Message string
}

func (erre *EncodingError) Error() string {
return erre.Context + ": error while encoding JSON request body (" + erre.Message + ")"
}

// NewEncodingError -
func NewEncodingError(ctx string, err error) *EncodingError {
return &EncodingError{Context: ctx, Message: err.Error()}
}

func parseErrorInfo(r io.Reader, ctx string) error {
erri := ErrorInfo{Context: ctx}

Expand Down Expand Up @@ -357,7 +372,7 @@ func updateReference(ctx context.Context, serverID string, reference string) err
Reference: reference,
})
if err != nil {
return err
return NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s", leasewebAPIURL, serverID)
Expand Down Expand Up @@ -388,7 +403,7 @@ func updateReverseLookup(ctx context.Context, serverID string, ip string, revers
ReverseLookup: reverseLookup,
})
if err != nil {
return err
return NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s/ips/%s", leasewebAPIURL, serverID, ip)
Expand Down Expand Up @@ -459,7 +474,7 @@ func addDHCPLease(ctx context.Context, serverID string, bootfile string) error {
Bootfile: bootfile,
})
if err != nil {
return err
return NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s/leases", leasewebAPIURL, serverID)
Expand Down Expand Up @@ -586,7 +601,7 @@ func createDedicatedServerNotificationSetting(ctx context.Context, serverID stri
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(notificationSetting)
if err != nil {
return nil, err
return nil, NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s/notificationSettings/%s", leasewebAPIURL, serverID, notificationType)
Expand Down Expand Up @@ -645,7 +660,7 @@ func updateDedicatedServerNotificationSetting(ctx context.Context, serverID stri
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(notificationSetting)
if err != nil {
return nil, err
return nil, NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s/notificationSettings/%s/%s", leasewebAPIURL, serverID, notificationType, notificationSettingID)
Expand Down Expand Up @@ -698,7 +713,7 @@ func createDedicatedServerCredential(ctx context.Context, serverID string, crede
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(credential)
if err != nil {
return nil, err
return nil, NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s/credentials", leasewebAPIURL, serverID)
Expand Down Expand Up @@ -761,7 +776,7 @@ func updateDedicatedServerCredential(ctx context.Context, serverID string, crede
Password: credential.Password,
})
if err != nil {
return nil, err
return nil, NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s/credentials/%s/%s", leasewebAPIURL, serverID, credential.Type, credential.Username)
Expand Down Expand Up @@ -887,7 +902,7 @@ func launchInstallationJob(ctx context.Context, serverID string, payload *Payloa
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(payload)
if err != nil {
return nil, err
return nil, NewEncodingError(apiCtx, err)
}

url := fmt.Sprintf("%s/bareMetals/v2/servers/%s/install", leasewebAPIURL, serverID)
Expand Down

0 comments on commit a6dc796

Please sign in to comment.