Skip to content

Commit

Permalink
Fix CLI Logging and Error Handling (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshayBhansali18 authored Oct 7, 2021
1 parent da87dbd commit 2af417b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</a>
</p>

This repo would server as an interface between the different CRDA clients and the platform. It contains tools that will be used by clients inorder to generate required input for platform APIs. One such tool is `gomanifest`.
This repo would serve as an interface between the different CRDA clients and the platform. It contains tools that will be used by clients inorder to generate required input for platform APIs. One such tool is `gomanifest`.
### Tools and Packages:

* `CRDA Cli`: CLI Tools to interact with CRDA Platform. [Learn more](docs/cli_README.md)
Expand Down
1 change: 1 addition & 0 deletions cmd/analyse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var verboseOut bool
var analyseCmd = &cobra.Command{
Use: "analyse",
Short: "Get detailed report of vulnerabilities.",
SilenceUsage: true,
Long: `Get detailed report of vulnerabilities. Supported ecosystems are Pypi (Python), Maven (Java), Npm (Node) and Golang (Go).
If stack has Vulnerabilities, command will exit with status code 2.`,
Args: validateFileArg,
Expand Down
22 changes: 18 additions & 4 deletions pkg/analyses/stackanalyses/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
Expand Down Expand Up @@ -153,8 +154,6 @@ func (mc *Controller) getRequest(requestParams driver.RequestType, postResponse
// validatePostResponse validates Stack Analyses POST API Response.
func (mc *Controller) validatePostResponse(apiResponse *http.Response) (*driver.PostResponseType, error) {
log.Debug().Msgf("Executing validatePostResponse.")
var body driver.PostResponseType
err := json.NewDecoder(apiResponse.Body).Decode(&body)

// In Case of Authentication Failure, json is not return from API, Need to catch before decoding.
if apiResponse.StatusCode == http.StatusForbidden {
Expand All @@ -163,7 +162,11 @@ func (mc *Controller) validatePostResponse(apiResponse *http.Response) (*driver.
return nil, fmt.Errorf("invalid authentication token")
}

var body driver.PostResponseType
err := json.NewDecoder(apiResponse.Body).Decode(&body)

if err != nil {

return nil, err
}
if apiResponse.StatusCode != http.StatusOK {
Expand All @@ -179,7 +182,18 @@ func (mc *Controller) validatePostResponse(apiResponse *http.Response) (*driver.
func (mc *Controller) validateGetResponse(apiResponse *http.Response) (*driver.GetResponseType, error) {
log.Debug().Msgf("Executing validateGetResponse.")
var body driver.GetResponseType
err := json.NewDecoder(apiResponse.Body).Decode(&body)
var buf bytes.Buffer

//use TeeReader to duplicate the contents of the Response Body of type io.ReaderCloser since data is streamed from the response body.
r := io.TeeReader(apiResponse.Body, &buf)
responseBodyContents, _ :=ioutil.ReadAll(r)
err := json.NewDecoder(&buf).Decode(&body)

if err != nil {
log.Error().Msg("analyse failed: Stack Analyses Get Request Failed. Please retry after sometime. If issue persists, Please raise at https://github.com/fabric8-analytics/cli-tools/issues.")
return nil, fmt.Errorf("Message from Server: "+string(responseBodyContents))
}

if apiResponse.StatusCode != http.StatusOK {
log.Debug().Msgf("Status from Server: %d", apiResponse.StatusCode)
log.Error().Msgf("Stack Analyses Get Request Failed with status code %d. Please retry after sometime. If issue persists, Please raise at https://github.com/fabric8-analytics/cli-tools/issues.\"", apiResponse.StatusCode)
Expand Down Expand Up @@ -211,7 +225,7 @@ func GetMatcher(manifestFile string) (driver.StackAnalysisInterface, error) {
return matcher, nil
}
}
return nil, errors.New("ecosystem not supported yet")
return nil, errors.New("analyse failed: \""+manifestFile+"\" does not appear to be a supported dependency manifest file. Supported manifest files include \"pom.xml\", \"package.json\", \"go.mod\", \"requirements.txt\". Please provide the path of a valid manifest file for analysis. ")
}

func (mc *Controller) buildFileStats(manifestFile string) *driver.ReadManifestResponse {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package utils

// Flag Defaults
const (
CRDAHost string = "https://f8a-analytics-2445582058137.production.gw.apicast.io"
CRDAAuthToken string = "9e7da76708fe374d8c10fa752e72989f"
CRDAHost string = "https://gw.api.openshift.io"
CRDAAuthToken string = "207c527cfc2a6b8dcf4fa43ad7a976da"
Debug bool = false
ActualHost string = "https://recommender.api.openshift.io"
)

0 comments on commit 2af417b

Please sign in to comment.