Skip to content

Commit

Permalink
feat: Add custom retryablehttp error handler (#423)
Browse files Browse the repository at this point in the history
* feat: Add retryclient error handler

* chore: Update low balance error messages

* test: Disable telemetry and API host in tests

* chore: Remove redundant environment variable

* test: Fix incorrect mediation option override
  • Loading branch information
bgins authored Nov 1, 2024
1 parent d30b6d8 commit 711ae57
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ jobs:
- name: Run tests
env:
LOG_LEVEL: debug
DISABLE_TELEMETRY: true
run: ./stack integration-tests

- name: Display resource provider logs
Expand Down
10 changes: 10 additions & 0 deletions pkg/http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,5 +429,15 @@ func newRetryClient() *retryablehttp.Client {
Msgf("")
}
}
// Return custom error with response body
retryClient.ErrorHandler = func(resp *http.Response, err error, numTries int) (*http.Response, error) {
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("%s %s gave up after %d attempt(s): %s", resp.Request.Method, resp.Request.URL, numTries, err)
}

return nil, fmt.Errorf("%s %s gave up after %d attempt(s): %s", resp.Request.Method, resp.Request.URL, numTries, string(body))
}
return retryClient
}
4 changes: 2 additions & 2 deletions pkg/solver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (controller *SolverController) addResourceOffer(resourceOffer data.Resource

// If the balance is less than the required balance, don't add the resource offer
if balance.Cmp(requiredBalanceWei) < 0 {
return nil, fmt.Errorf("address %s doesn't have enough funds. required balance is %s but expected balance is %s", resourceOffer.ResourceProvider, requiredBalanceWei, balance)
return nil, fmt.Errorf("address %s doesn't have enough ETH balance. The required balance is %s but current balance is %s", resourceOffer.ResourceProvider, requiredBalanceWei, balance)
}

// required LP balance
Expand All @@ -361,7 +361,7 @@ func (controller *SolverController) addResourceOffer(resourceOffer data.Resource
return nil, fmt.Errorf("failed to retrieve LP balance for resource provider: %v", err)
}
if balanceLp.Cmp(requiredBalanceLp) < 0 {
return nil, fmt.Errorf("address %s doesn't have enough LP balance. required balance is %s but expected balance is %s", resourceOffer.ResourceProvider, requiredBalanceLp, balanceLp)
return nil, fmt.Errorf("address %s doesn't have enough LP balance. The required balance is %s but current balance is %s", resourceOffer.ResourceProvider, requiredBalanceLp, balanceLp)
}

controller.log.Info("add resource offer", resourceOffer)
Expand Down
7 changes: 6 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ func getMediator(
) (*mediator.Mediator, error) {
mediatorOptions := optionsfactory.NewMediatorOptions()
mediatorOptions.Web3.PrivateKey = os.Getenv("MEDIATOR_PRIVATE_KEY")

if mediatorOptions.Web3.PrivateKey == "" {
return nil, fmt.Errorf("MEDIATOR_PRIVATE_KEY is not defined")
}
mediatorOptions, err := optionsfactory.ProcessMediatorOptions(mediatorOptions, "dev")
if err != nil {
return nil, err
}
mediatorOptions.Services.APIHost = ""

noopTracer := traceNoop.NewTracerProvider().Tracer(system.GetOTelServiceName(system.MediatorService))
web3SDK, err := web3.NewContractSDK(systemContext.Ctx, mediatorOptions.Web3, noopTracer)
Expand All @@ -58,6 +60,7 @@ func getMediator(
func getJobCreatorOptions(options testOptions) (jobcreator.JobCreatorOptions, error) {
jobCreatorOptions := optionsfactory.NewJobCreatorOptions()
jobCreatorOptions.Web3.PrivateKey = os.Getenv("JOB_CREATOR_PRIVATE_KEY")

if jobCreatorOptions.Web3.PrivateKey == "" {
return jobCreatorOptions, fmt.Errorf("JOB_CREATOR_PRIVATE_KEY is not defined")
}
Expand All @@ -70,7 +73,9 @@ func getJobCreatorOptions(options testOptions) (jobcreator.JobCreatorOptions, er
return ret, err
}

jobCreatorOptions.Mediation.CheckResultsPercentage = options.moderationChance
ret.Mediation.CheckResultsPercentage = options.moderationChance
ret.Telemetry.Disable = true
ret.Offer.Services.APIHost = ""
return ret, nil
}

Expand Down

0 comments on commit 711ae57

Please sign in to comment.