src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

errors.go (2965B)


      1 // Copyright 2022 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package scan
      6 
      7 import (
      8 	"errors"
      9 	"strings"
     10 )
     11 
     12 //lint:file-ignore ST1005 Ignore staticcheck message about error formatting
     13 var (
     14 	// errVulnerabilitiesFound indicates that vulnerabilities were detected
     15 	// when running govulncheck. This returns exit status 3 when running
     16 	// without the -json flag.
     17 	errVulnerabilitiesFound = &exitCodeError{message: "vulnerabilities found", code: 3}
     18 
     19 	// errHelp indicates that usage help was requested.
     20 	errHelp = &exitCodeError{message: "help requested", code: 0}
     21 
     22 	// errUsage indicates that there was a usage error on the command line.
     23 	//
     24 	// In this case, we assume that the user does not know how to run
     25 	// govulncheck and exit with status 2.
     26 	errUsage = &exitCodeError{message: "invalid usage", code: 2}
     27 
     28 	// errNoPatterns indicates that no package patterns were provided if the scan level is WantPackages.
     29 	errNoPatterns = &exitCodeError{
     30 		message: "no package patterns provided\n\nTo scan the current module, run: govulncheck ./...",
     31 		code:    2,
     32 	}
     33 
     34 	// errNoPackagesMatched indicates that the provided patterns matched no packages if scan level is WantPackages.
     35 	errNoPackagesMatched = &exitCodeError{
     36 		message: "no packages matched the provided patterns",
     37 		code:    2,
     38 	}
     39 
     40 	// errGoVersionMismatch is used to indicate that there is a mismatch between
     41 	// the Go version used to build govulncheck and the one currently on PATH.
     42 	errGoVersionMismatch = errors.New(`Loading packages failed, possibly due to a mismatch between the Go version
     43 used to build govulncheck and the Go version on PATH. Consider rebuilding
     44 govulncheck with the current Go version.`)
     45 
     46 	// errNoGoMod indicates that a go.mod file was not found in this module.
     47 	errNoGoMod = errors.New(`no go.mod file
     48 
     49 govulncheck only works with Go modules. Try navigating to your module directory.
     50 Otherwise, run go mod init to make your project a module.
     51 
     52 See https://go.dev/doc/modules/managing-dependencies for more information.`)
     53 
     54 	// errNoBinaryFlag indicates that govulncheck was run on a file, without
     55 	// the -mode=binary flag.
     56 	errNoBinaryFlag = errors.New(`By default, govulncheck runs source analysis on Go modules.
     57 
     58 Did you mean to run govulncheck with -mode=binary?
     59 
     60 For details, run govulncheck -h.`)
     61 )
     62 
     63 type exitCodeError struct {
     64 	message string
     65 	code    int
     66 }
     67 
     68 func (e *exitCodeError) Error() string { return e.message }
     69 func (e *exitCodeError) ExitCode() int { return e.code }
     70 
     71 // isGoVersionMismatchError checks if err is due to mismatch between
     72 // the Go version used to build govulncheck and the one currently
     73 // on PATH.
     74 func isGoVersionMismatchError(err error) bool {
     75 	msg := err.Error()
     76 	// See golang.org/x/tools/go/packages/packages.go.
     77 	return strings.Contains(msg, "This application uses version go") &&
     78 		strings.Contains(msg, "It may fail to process source files")
     79 }