Improved error handling in vchasno responses

This commit is contained in:
2026-01-16 02:27:09 +03:00
parent 28a8810ffb
commit 861a1640d4
2 changed files with 30 additions and 0 deletions

View File

@@ -8,6 +8,15 @@ import (
"log"
)
type APIError struct {
Code int
Message string
}
func (e *APIError) Error() string {
return fmt.Sprintf("vchasno api error %d: %s", e.Code, e.Message)
}
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
request.Device = c.device
@@ -46,6 +55,16 @@ func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, resp
return fmt.Errorf("failed to unmarshal response: %w, body: %s", err, string(body))
}
var baseResp BaseResponse
if err := json.Unmarshal(body, &baseResp); err == nil {
if baseResp.HasError() {
return &APIError{
Code: baseResp.Res,
Message: baseResp.Errortxt,
}
}
}
return nil
}