code-intelligence.com/cifuzz@v0.40.0/internal/api/error_details.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"net/url"
     7  
     8  	"github.com/pkg/errors"
     9  
    10  	"code-intelligence.com/cifuzz/pkg/finding"
    11  	"code-intelligence.com/cifuzz/pkg/log"
    12  )
    13  
    14  type errorDetailsJSON struct {
    15  	VersionSchema int                    `json:"version_schema"`
    16  	ErrorDetails  []finding.ErrorDetails `json:"error_details"`
    17  }
    18  
    19  // GetErrorDetails gets the error details from the API
    20  func (client *APIClient) GetErrorDetails(token string) ([]finding.ErrorDetails, error) {
    21  	// get it from the API
    22  	url, err := url.JoinPath("v2", "error-details")
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	resp, err := client.sendRequest("GET", url, nil, token)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	defer resp.Body.Close()
    32  
    33  	if resp.StatusCode != 200 {
    34  		// the request did not succeed, but we don't want the entire process to fail
    35  		// so we just log the error and return an empty list
    36  		if resp.StatusCode == 401 {
    37  			log.Warnf("Not authorized to get error details. Please log in to enable this feature.")
    38  		} else {
    39  			log.Warnf("Failed to get error details: %s", resp.Status)
    40  			log.Infof("Response: %s", resp.Body)
    41  		}
    42  		log.Infof("Continuing without external error details")
    43  		return nil, nil
    44  	}
    45  
    46  	body, err := io.ReadAll(resp.Body)
    47  	if err != nil {
    48  		return nil, errors.WithStack(err)
    49  	}
    50  
    51  	var errorDetailsFromJSON errorDetailsJSON
    52  	err = json.Unmarshal(body, &errorDetailsFromJSON)
    53  	if err != nil {
    54  		return nil, errors.WithStack(err)
    55  	}
    56  
    57  	return errorDetailsFromJSON.ErrorDetails, nil
    58  }