Home > Blockchain >  Showing the error after executing the entire code
Showing the error after executing the entire code

Time:08-30

I have a loop in which the values are sorted, as well as the comparison of these values. If there are identical values, they are removed from the same array of values. After that, I want to do the following so that the code continues to run until the end. And after that, inform users that these values (which were deleted) were not added. But I wrote the code, so this causes the execution of further code to stop. Tell me how to make it right so that even if the same values meet, the code works to the end and only then output messages about these identical values. In addition: I give the structure in json without duplicates. But I also want to give information that errors have occurred. Tell me how to implement this moment correctly

    type Result struct {
        Result  int                        `json:"result"`
        Message string                     `json:"message"`
        Status []string
    }

    var result Result

var searchProject = make([]struct{
            ProjectId string
        }, 0)

        var query string
        switch Type {
        case sku:
            query = fmt.Sprintf(`
                SELECT s.project_id
                FROM sku.sku_projects s
                WHERE s.deleted_at IS NULL
                AND s.sku_id = %d
                AND s.project_id IN (%s)
            `, val.FieldByName("SkuID").Int(), ProjectId)
        case user:
            query = fmt.Sprintf(`
                SELECT u.project_id
                FROM users.user_project u
                WHERE u.deleted_at IS NULL
                AND u.user_id = %d
                AND u.project_id IN (%s)
            `, val.FieldByName("UserID").Int(), ProjectId)
        case reason:
            query = fmt.Sprintf(`
                SELECT r.project_id
                FROM reasons.reason_projects r
                WHERE r.deleted_at IS NULL
                AND r.reason_id = %d
                AND r.project_id IN (%s)
            `, val.FieldByName("ReasonID").Int(), ProjectId)
        default:
            http.NotFound(w, r)
            return
        }
        _ = DB.Raw(query).Scan(&searchProject)

        key := make([]int, 0)
        double := make([]string, 0)
        if len(searchProject) > 0{
            for _, v := range searchProject{
                for i, value := range Projects{
                    if value == v.ProjectId{
                        key = append(key, i)
                        double = append(double, value)
                    }
                }
            }
            for _, v := range key{
                Projects = append(Projects[:v], Projects[v 1:]...)
            }
        }
            if len(key) > 0{
            if (len(Projects) != len(searchProject)) {
                http.Error(w, ("You are connecting already existing bindings"), http.StatusBadRequest)
                return
            }else {
                result.Result = 0
                result.Message = "The following project bindings already exist:"
                result.Status = double
                utils.WriteJSON(w, result)
            }
        }
        utils.WriteJSON(w, &Struct)
    }
}

CodePudding user response:

Try to use this library from hashicorp. Basically you need to collect all the errors into an array and then return it

CodePudding user response:

Using multierr

var err error

// ... your logic

// where I assume you want to error
if (len(Projects) != len(searchProject)) {
  err = multierr.Append(err, errors.New("you new error message"))
}


// when you return a reply
http.Error(w, err.Error(), http.StatusBadRequest)


  •  Tags:  
  • go
  • Related