2 Commits

3 changed files with 31 additions and 1 deletions

View File

@@ -40,6 +40,6 @@ func NewDMClient(token, dmURL, device string) *Client {
device: device, device: device,
resty: restyClient, resty: restyClient,
apiBaseURL: dmURL, apiBaseURL: dmURL,
fiscalEndpoint: "/dm/fiscal", fiscalEndpoint: "/dm/execute",
} }
} }

View File

@@ -8,6 +8,15 @@ import (
"log" "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 { func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
request.Device = c.device 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)) 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 return nil
} }

View File

@@ -15,6 +15,17 @@ type BaseResponse struct {
ErrorExtra interface{} `json:"error_extra"` ErrorExtra interface{} `json:"error_extra"`
} }
func (r *BaseResponse) HasError() bool {
return r.Res != 0
}
func (r *BaseResponse) Error() string {
if r.Errortxt != "" {
return r.Errortxt
}
return ""
}
type SellResponse struct { type SellResponse struct {
BaseResponse BaseResponse
Info SellInfo `json:"info"` Info SellInfo `json:"info"`