mirror of https://github.com/daffainfo/nuclei.git
refactor: Wrap errors using %w in fmt.Errorf
see: * https://github.com/xxpxxxxp/intellij-plugin-golangci-lint/blob/master/explanation/goerr113.md * https://go.dev/blog/go1.13-errors#wrapping-errors-with-wdev
parent
f9c214a66f
commit
fdd22ab668
|
@ -28,7 +28,7 @@ func loadProxyServers(options *types.Options) error {
|
||||||
} else if fileutil.FileExists(p) {
|
} else if fileutil.FileExists(p) {
|
||||||
file, err := os.Open(p)
|
file, err := os.Open(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not open proxy file: %s", err)
|
return fmt.Errorf("could not open proxy file: %w", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
|
@ -242,12 +242,12 @@ func (r *Runner) getLatestReleaseFromGithub(latestTag string) (*github.Repositor
|
||||||
func (r *Runner) downloadReleaseAndUnzip(ctx context.Context, version, downloadURL string) (*templateUpdateResults, error) {
|
func (r *Runner) downloadReleaseAndUnzip(ctx context.Context, version, downloadURL string) (*templateUpdateResults, error) {
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create HTTP request to %s: %s", downloadURL, err)
|
return nil, fmt.Errorf("failed to create HTTP request to %s: %w", downloadURL, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to download a release file from %s: %s", downloadURL, err)
|
return nil, fmt.Errorf("failed to download a release file from %s: %w", downloadURL, err)
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
|
@ -256,23 +256,23 @@ func (r *Runner) downloadReleaseAndUnzip(ctx context.Context, version, downloadU
|
||||||
|
|
||||||
buf, err := ioutil.ReadAll(res.Body)
|
buf, err := ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create buffer for zip file: %s", err)
|
return nil, fmt.Errorf("failed to create buffer for zip file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
reader := bytes.NewReader(buf)
|
reader := bytes.NewReader(buf)
|
||||||
zipReader, err := zip.NewReader(reader, reader.Size())
|
zipReader, err := zip.NewReader(reader, reader.Size())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to uncompress zip file: %s", err)
|
return nil, fmt.Errorf("failed to uncompress zip file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the template folder if it doesn't exist
|
// Create the template folder if it doesn't exist
|
||||||
if err := os.MkdirAll(r.templatesConfig.TemplatesDirectory, 0755); err != nil {
|
if err := os.MkdirAll(r.templatesConfig.TemplatesDirectory, 0755); err != nil {
|
||||||
return nil, fmt.Errorf("failed to create template base folder: %s", err)
|
return nil, fmt.Errorf("failed to create template base folder: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err := r.compareAndWriteTemplates(zipReader)
|
results, err := r.compareAndWriteTemplates(zipReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to write templates: %s", err)
|
return nil, fmt.Errorf("failed to write templates: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.options.Verbose {
|
if r.options.Verbose {
|
||||||
|
@ -342,7 +342,7 @@ func (r *Runner) compareAndWriteTemplates(zipReader *zip.Reader) (*templateUpdat
|
||||||
|
|
||||||
relativeTemplatePath, err := filepath.Rel(configuredTemplateDirectory, templateAbsolutePath)
|
relativeTemplatePath, err := filepath.Rel(configuredTemplateDirectory, templateAbsolutePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not calculate relative path for template: %s. %s", templateAbsolutePath, err)
|
return nil, fmt.Errorf("could not calculate relative path for template: %s. %w", templateAbsolutePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if isAddition {
|
if isAddition {
|
||||||
|
@ -369,13 +369,13 @@ func (r *Runner) compareAndWriteTemplates(zipReader *zip.Reader) (*templateUpdat
|
||||||
func writeUnZippedTemplateFile(err error, templateAbsolutePath string, zipTemplateFile *zip.File) (string, error) {
|
func writeUnZippedTemplateFile(err error, templateAbsolutePath string, zipTemplateFile *zip.File) (string, error) {
|
||||||
templateFile, err := os.OpenFile(templateAbsolutePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
templateFile, err := os.OpenFile(templateAbsolutePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("could not create template file: %s", err)
|
return "", fmt.Errorf("could not create template file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
zipTemplateFileReader, err := zipTemplateFile.Open()
|
zipTemplateFileReader, err := zipTemplateFile.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = templateFile.Close()
|
_ = templateFile.Close()
|
||||||
return "", fmt.Errorf("could not open archive to extract file: %s", err)
|
return "", fmt.Errorf("could not open archive to extract file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
md5Hash := md5.New()
|
md5Hash := md5.New()
|
||||||
|
@ -383,11 +383,11 @@ func writeUnZippedTemplateFile(err error, templateAbsolutePath string, zipTempla
|
||||||
// Save file and also read into hash.Hash for md5
|
// Save file and also read into hash.Hash for md5
|
||||||
if _, err := io.Copy(templateFile, io.TeeReader(zipTemplateFileReader, md5Hash)); err != nil {
|
if _, err := io.Copy(templateFile, io.TeeReader(zipTemplateFileReader, md5Hash)); err != nil {
|
||||||
_ = templateFile.Close()
|
_ = templateFile.Close()
|
||||||
return "", fmt.Errorf("could not write template file: %s", err)
|
return "", fmt.Errorf("could not write template file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := templateFile.Close(); err != nil {
|
if err := templateFile.Close(); err != nil {
|
||||||
return "", fmt.Errorf("could not close file newly created template file: %s", err)
|
return "", fmt.Errorf("could not close file newly created template file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
checksum := hex.EncodeToString(md5Hash.Sum(nil))
|
checksum := hex.EncodeToString(md5Hash.Sum(nil))
|
||||||
|
@ -411,7 +411,7 @@ func calculateTemplateAbsolutePath(zipFilePath, configuredTemplateDirectory stri
|
||||||
templateDirectory := filepath.Join(configuredTemplateDirectory, relativeDirectoryPathWithoutZipRoot)
|
templateDirectory := filepath.Join(configuredTemplateDirectory, relativeDirectoryPathWithoutZipRoot)
|
||||||
|
|
||||||
if err := os.MkdirAll(templateDirectory, 0755); err != nil {
|
if err := os.MkdirAll(templateDirectory, 0755); err != nil {
|
||||||
return "", false, fmt.Errorf("failed to create template folder: %s. %s", templateDirectory, err)
|
return "", false, fmt.Errorf("failed to create template folder: %s. %w", templateDirectory, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(templateDirectory, fileName), false, nil
|
return filepath.Join(templateDirectory, fileName), false, nil
|
||||||
|
|
|
@ -124,7 +124,7 @@ func (r *requestGenerator) makeSelfContainedRequest(dynamicValues map[string]int
|
||||||
reader := bufio.NewReader(strings.NewReader(data))
|
reader := bufio.NewReader(strings.NewReader(data))
|
||||||
s, err := reader.ReadString('\n')
|
s, err := reader.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not read request: %s", err)
|
return nil, fmt.Errorf("could not read request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(s, " ")
|
parts := strings.Split(s, " ")
|
||||||
|
@ -133,7 +133,7 @@ func (r *requestGenerator) makeSelfContainedRequest(dynamicValues map[string]int
|
||||||
}
|
}
|
||||||
parsed, err := url.Parse(parts[1])
|
parsed, err := url.Parse(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not parse request URL: %s", err)
|
return nil, fmt.Errorf("could not parse request URL: %w", err)
|
||||||
}
|
}
|
||||||
values := generators.MergeMaps(
|
values := generators.MergeMaps(
|
||||||
generators.MergeMaps(dynamicValues, generateVariables(parsed, false)),
|
generators.MergeMaps(dynamicValues, generateVariables(parsed, false)),
|
||||||
|
|
|
@ -33,7 +33,7 @@ func Parse(request, baseURL string, unsafe bool) (*Request, error) {
|
||||||
|
|
||||||
parsedURL, err := url.Parse(baseURL)
|
parsedURL, err := url.Parse(baseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not parse request URL: %s", err)
|
return nil, fmt.Errorf("could not parse request URL: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if unsafe {
|
if unsafe {
|
||||||
|
@ -42,7 +42,7 @@ func Parse(request, baseURL string, unsafe bool) (*Request, error) {
|
||||||
reader := bufio.NewReader(strings.NewReader(request))
|
reader := bufio.NewReader(strings.NewReader(request))
|
||||||
s, err := reader.ReadString('\n')
|
s, err := reader.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not read request: %s", err)
|
return nil, fmt.Errorf("could not read request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(s, " ")
|
parts := strings.Split(s, " ")
|
||||||
|
@ -101,7 +101,7 @@ func Parse(request, baseURL string, unsafe bool) (*Request, error) {
|
||||||
if !unsafe && strings.HasPrefix(parts[1], "http") {
|
if !unsafe && strings.HasPrefix(parts[1], "http") {
|
||||||
parsed, parseErr := url.Parse(parts[1])
|
parsed, parseErr := url.Parse(parts[1])
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
return nil, fmt.Errorf("could not parse request URL: %s", parseErr)
|
return nil, fmt.Errorf("could not parse request URL: %w", parseErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawRequest.Path = parsed.Path
|
rawRequest.Path = parsed.Path
|
||||||
|
@ -133,7 +133,7 @@ func Parse(request, baseURL string, unsafe bool) (*Request, error) {
|
||||||
// Set the request body
|
// Set the request body
|
||||||
b, err := ioutil.ReadAll(reader)
|
b, err := ioutil.ReadAll(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not read request body: %s", err)
|
return nil, fmt.Errorf("could not read request body: %w", err)
|
||||||
}
|
}
|
||||||
rawRequest.Data = string(b)
|
rawRequest.Data = string(b)
|
||||||
if !mutlipartRequest {
|
if !mutlipartRequest {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/andygrunwald/go-jira"
|
"github.com/andygrunwald/go-jira"
|
||||||
|
|
||||||
"github.com/projectdiscovery/gologger"
|
"github.com/projectdiscovery/gologger"
|
||||||
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
|
||||||
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
||||||
|
@ -103,7 +104,7 @@ func (i *Integration) CreateNewIssue(event *output.ResultEvent) error {
|
||||||
d, _ := ioutil.ReadAll(resp.Body)
|
d, _ := ioutil.ReadAll(resp.Body)
|
||||||
data = string(d)
|
data = string(d)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("%s => %s", err, data)
|
return fmt.Errorf("%w => %s", err, data)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -140,7 +141,7 @@ func (i *Integration) FindExistingIssue(event *output.ResultEvent) (string, erro
|
||||||
d, _ := ioutil.ReadAll(resp.Body)
|
d, _ := ioutil.ReadAll(resp.Body)
|
||||||
data = string(d)
|
data = string(d)
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("%s => %s", err, data)
|
return "", fmt.Errorf("%w => %s", err, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch resp.Total {
|
switch resp.Total {
|
||||||
|
|
Loading…
Reference in New Issue