refactor: streamline error handling and improve HTTP request management

- Removed the print flag from handleError function, simplifying error logging based on the development environment.
- Introduced a new performRequest function for standardized HTTP requests with JSON bodies, enhancing code reusability.
- Updated SendIMNotification and related functions to utilize the new RequestParam type for better clarity and consistency.
- Normalized HTTP request paths in the createHttpRequest method to ensure correct URL formatting.
- Added detailed error logging for JSON unmarshaling failures in syncFiles method.
- Introduced a NewHttpClient function to create HTTP clients with customizable timeouts.
This commit is contained in:
Marvin Zhang
2024-12-21 11:27:58 +08:00
parent 3cb74d76f9
commit c897fb58e4
8 changed files with 113 additions and 78 deletions

View File

@@ -309,6 +309,11 @@ func (r *Runner) configureEnv() {
}
func (r *Runner) createHttpRequest(method, path string) (*http.Response, error) {
// Normalize path
if strings.HasPrefix(path, "/") {
path = path[1:]
}
// Construct master URL
var id string
if r.s.GitId.IsZero() {
@@ -316,7 +321,7 @@ func (r *Runner) createHttpRequest(method, path string) (*http.Response, error)
} else {
id = r.s.GitId.Hex()
}
url := fmt.Sprintf("%s/sync/%s%s", utils.GetApiEndpoint(), id, path)
url := fmt.Sprintf("%s/sync/%s/%s", utils.GetApiEndpoint(), id, path)
// Create and execute request
req, err := http.NewRequest(method, url, nil)
@@ -357,7 +362,8 @@ func (r *Runner) syncFiles() (err error) {
var masterFiles map[string]entity.FsFileInfo
err = json.Unmarshal(body, &masterFiles)
if err != nil {
log.Errorf("error unmarshaling JSON: %v", err)
log.Errorf("error unmarshaling JSON for URL: %s", resp.Request.URL.String())
log.Errorf("error details: %v", err)
return err
}

View File

@@ -239,7 +239,7 @@ func (svc *Service) getRunner(taskId primitive.ObjectID) (r interfaces.TaskRunne
}
func (svc *Service) addRunner(taskId primitive.ObjectID, r interfaces.TaskRunner) {
log.Debugf("[TaskHandlerService] addRunner: taskId[%v]", taskId)
log.Debugf("[TaskHandlerService] addRunner: taskId[%s]", taskId.Hex())
svc.runners.Store(taskId, r)
}