chore: use errors.New to replace fmt.Errorf with no parameters (#4204)

Signed-off-by: RiceChuan <lc582041246@gmail.com>
This commit is contained in:
leo 2024-12-24 13:17:00 +08:00 committed by GitHub
parent d54d20abea
commit b287d6419b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 12 deletions

View File

@ -38,7 +38,7 @@ func Error2(v interface{}, err error) error {
func envFile() (string, error) { func envFile() (string, error) {
if file := os.Getenv("GOENV"); file != "" { if file := os.Getenv("GOENV"); file != "" {
if file == "off" { if file == "off" {
return "", fmt.Errorf("GOENV=off") return "", errors.New("GOENV=off")
} }
return file, nil return file, nil
} }
@ -47,7 +47,7 @@ func envFile() (string, error) {
return "", err return "", err
} }
if dir == "" { if dir == "" {
return "", fmt.Errorf("missing user-config dir") return "", errors.New("missing user-config dir")
} }
return filepath.Join(dir, "go", "env"), nil return filepath.Join(dir, "go", "env"), nil
} }
@ -60,7 +60,7 @@ func GetRuntimeEnv(key string) (string, error) {
return "", err return "", err
} }
if file == "" { if file == "" {
return "", fmt.Errorf("missing runtime env file") return "", errors.New("missing runtime env file")
} }
var data []byte var data []byte
var runtimeEnv string var runtimeEnv string

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"go/build" "go/build"
@ -18,7 +19,7 @@ var directory = flag.String("pwd", "", "Working directory of Xray vformat.")
func envFile() (string, error) { func envFile() (string, error) {
if file := os.Getenv("GOENV"); file != "" { if file := os.Getenv("GOENV"); file != "" {
if file == "off" { if file == "off" {
return "", fmt.Errorf("GOENV=off") return "", errors.New("GOENV=off")
} }
return file, nil return file, nil
} }
@ -27,7 +28,7 @@ func envFile() (string, error) {
return "", err return "", err
} }
if dir == "" { if dir == "" {
return "", fmt.Errorf("missing user-config dir") return "", errors.New("missing user-config dir")
} }
return filepath.Join(dir, "go", "env"), nil return filepath.Join(dir, "go", "env"), nil
} }
@ -40,7 +41,7 @@ func GetRuntimeEnv(key string) (string, error) {
return "", err return "", err
} }
if file == "" { if file == "" {
return "", fmt.Errorf("missing runtime env file") return "", errors.New("missing runtime env file")
} }
var data []byte var data []byte
var runtimeEnv string var runtimeEnv string

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"go/build" "go/build"
@ -22,7 +23,7 @@ var directory = flag.String("pwd", "", "Working directory of Xray vprotogen.")
func envFile() (string, error) { func envFile() (string, error) {
if file := os.Getenv("GOENV"); file != "" { if file := os.Getenv("GOENV"); file != "" {
if file == "off" { if file == "off" {
return "", fmt.Errorf("GOENV=off") return "", errors.New("GOENV=off")
} }
return file, nil return file, nil
} }
@ -31,7 +32,7 @@ func envFile() (string, error) {
return "", err return "", err
} }
if dir == "" { if dir == "" {
return "", fmt.Errorf("missing user-config dir") return "", errors.New("missing user-config dir")
} }
return filepath.Join(dir, "go", "env"), nil return filepath.Join(dir, "go", "env"), nil
} }
@ -44,7 +45,7 @@ func GetRuntimeEnv(key string) (string, error) {
return "", err return "", err
} }
if file == "" { if file == "" {
return "", fmt.Errorf("missing runtime env file") return "", errors.New("missing runtime env file")
} }
var data []byte var data []byte
var runtimeEnv string var runtimeEnv string
@ -101,12 +102,12 @@ Download %s v%s or later from https://github.com/protocolbuffers/protobuf/releas
func getProjectProtocVersion(url string) (string, error) { func getProjectProtocVersion(url string) (string, error) {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
return "", fmt.Errorf("can not get the version of protobuf used in xray project") return "", errors.New("can not get the version of protobuf used in xray project")
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("can not read from body") return "", errors.New("can not read from body")
} }
versionRegexp := regexp.MustCompile(`\/\/\s*protoc\s*v\d+\.(\d+\.\d+)`) versionRegexp := regexp.MustCompile(`\/\/\s*protoc\s*v\d+\.(\d+\.\d+)`)
matched := versionRegexp.FindStringSubmatch(string(body)) matched := versionRegexp.FindStringSubmatch(string(body))

View File

@ -3,6 +3,7 @@ package api
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -101,7 +102,7 @@ func fetchHTTPContent(target string) ([]byte, error) {
content, err := buf.ReadAllToBytes(resp.Body) content, err := buf.ReadAllToBytes(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read HTTP response") return nil, errors.New("failed to read HTTP response")
} }
return content, nil return content, nil