Skip to content

Commit

Permalink
Add API error type
Browse files Browse the repository at this point in the history
  • Loading branch information
tnii authored and diegobernardes committed Jan 9, 2025
1 parent 1ce0d72 commit cbeecb6
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ctrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ctrader

import (
"context"
"errors"
"fmt"
"reflect"

Expand All @@ -23,6 +22,16 @@ func (e undefinedProtobufResourceError[T]) Error() string {
return fmt.Sprintf("undefined payload type '%d'", t.Elem())
}

type ProtoOAError struct {
ErrorCode string
Description string
MaintenanceEndTimestamp int64
}

func (e ProtoOAError) Error() string {
return fmt.Sprintf("%s: %s", e.ErrorCode, e.Description)
}

// Command is a helper function used to send a request and receive a response.
//
// nolint ireturn
Expand All @@ -33,7 +42,7 @@ func Command[A, B proto.Message](ctx context.Context, c *Client, req A) (B, erro
}
switch v := resp.(type) {
case *openapi.ProtoOAErrorRes:
return *new(B), errors.New("failed authenticate the account")
return *new(B), ProtoOAError{ErrorCode: v.GetErrorCode(), Description: v.GetDescription(), MaintenanceEndTimestamp: v.GetMaintenanceEndTimestamp()}
case B:
return v, nil
default:
Expand Down

0 comments on commit cbeecb6

Please sign in to comment.