Skip to content

Commit

Permalink
refactor: use errors new for simple strings
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic committed Sep 20, 2024
1 parent 93cfc5e commit b32b732
Show file tree
Hide file tree
Showing 24 changed files with 85 additions and 72 deletions.
3 changes: 2 additions & 1 deletion internal/cmd/tailscale/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package tailscale

import (
"errors"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -47,7 +48,7 @@ func GetConnection(profile *config.Profile) (*tsnet.Server, error) {
controlURL = fmt.Sprintf("http://localhost:%d", serverConfig.HeadscalePort)
} else {
if serverConfig.Frps == nil {
return nil, fmt.Errorf("frps config is missing")
return nil, errors.New("frps config is missing")
}
controlURL = util.GetFrpcHeadscaleUrl(serverConfig.Frps.Protocol, serverConfig.Id, serverConfig.Frps.Domain)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/testing/agent/mocks/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package mocks

import (
"fmt"
"errors"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -48,7 +48,7 @@ func NewMockRestServer(t *testing.T, workspace *workspace.Workspace) *httptest.S
{
gitproviderController.GET("/for-url/:url", func(ctx *gin.Context) {
// This simulates a non-configured git provider
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get git provider for url"))
ctx.AbortWithError(http.StatusInternalServerError, errors.New("failed to get git provider for url"))
})
}

Expand Down
8 changes: 4 additions & 4 deletions internal/util/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package util

import (
"fmt"
"errors"
"net/url"
"regexp"
"strings"
Expand All @@ -22,7 +22,7 @@ func GetValidatedName(input string) (string, error) {
}

if !matched {
return "", fmt.Errorf("only letters, numbers, and dashes are allowed")
return "", errors.New("only letters, numbers, and dashes are allowed")
}

return input, nil
Expand All @@ -31,13 +31,13 @@ func GetValidatedName(input string) (string, error) {
func GetValidatedUrl(input string) (string, error) {
// Check if the input starts with a scheme (e.g., http:// or https://)
if !strings.HasPrefix(input, "http://") && !strings.HasPrefix(input, "https://") {
return "", fmt.Errorf("input is missing http:// or https://")
return "", errors.New("input is missing http:// or https://")
}

// Try to parse the input as a URL
parsedURL, err := url.Parse(input)
if err != nil {
return "", fmt.Errorf("input is not a valid URL")
return "", errors.New("input is not a valid URL")
}

// If parsing was successful, return the fixed URL
Expand Down
3 changes: 1 addition & 2 deletions pkg/build/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package build
import (
"context"
"errors"
"fmt"
"os"

"github.com/daytonaio/daytona/pkg/build/detect"
Expand Down Expand Up @@ -35,7 +34,7 @@ func (b *DevcontainerBuilder) Build(build Build) (string, string, error) {
}

if builderType != detect.BuilderTypeDevcontainer {
return "", "", fmt.Errorf("failed to detect devcontainer config")
return "", "", errors.New("failed to detect devcontainer config")
}

return b.buildDevcontainer(build)
Expand Down
3 changes: 2 additions & 1 deletion pkg/build/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package build

import (
"errors"
"fmt"

"github.com/daytonaio/daytona/pkg/containerregistry"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (f *BuilderFactory) Create(build Build, projectDir string) (IBuilder, error

func (f *BuilderFactory) CheckExistingBuild(b Build) (*Build, error) {
if b.Repository == nil {
return nil, fmt.Errorf("repository must be set")
return nil, errors.New("repository must be set")
}

build, err := f.buildStore.Find(&Filter{
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/ports/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ports
import (
"context"
"encoding/base64"
"errors"
"fmt"
"hash/fnv"
"strconv"
Expand Down Expand Up @@ -111,7 +112,7 @@ func ForwardPublicPort(workspaceId, projectName string, hostPort, targetPort uin
subDomain := fmt.Sprintf("%d-%s", targetPort, base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprint(h.Sum64()))))

if serverConfig.Frps == nil {
return fmt.Errorf("frps config is missing")
return errors.New("frps config is missing")
}

go func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/projectconfig/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var projectConfigAddCmd = &cobra.Command{

func RunProjectConfigAddFlow(apiClient *apiclient.APIClient, gitProviders []apiclient.GitProvider, ctx context.Context) (*apiclient.ProjectConfig, error) {
if workspace_util.CheckAnyProjectConfigurationFlagSet(projectConfigurationFlags) {
return nil, fmt.Errorf("please provide the repository URL in order to set up custom project config details through the CLI")
return nil, errors.New("please provide the repository URL in order to set up custom project config details through the CLI")
}

var createDtos []apiclient.CreateProjectDTO
Expand Down
11 changes: 6 additions & 5 deletions pkg/cmd/server/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package daemon

import (
"bufio"
"errors"
"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -82,9 +83,9 @@ func Start(logFilePath string) error {
}

if status == service.StatusStopped {
return fmt.Errorf("daemon stopped unexpectedly")
return errors.New("daemon stopped unexpectedly")
} else {
return fmt.Errorf("daemon status unknown")
return errors.New("daemon status unknown")
}
}

Expand All @@ -109,7 +110,7 @@ func Stop() error {
func getServiceConfig() (*service.Config, error) {
user, ok := os.LookupEnv("USER")
if !ok {
return nil, fmt.Errorf("could not determine user")
return nil, errors.New("could not determine user")
}

svcConfig := &service.Config{
Expand All @@ -121,7 +122,7 @@ func getServiceConfig() (*service.Config, error) {

switch runtime.GOOS {
case "windows":
return nil, fmt.Errorf("daemon mode not supported on Windows")
return nil, errors.New("daemon mode not supported on Windows")
case "linux":
// Fix for running as root on Linux
if user == "root" {
Expand Down Expand Up @@ -160,5 +161,5 @@ func getServiceFilePath(cfg *service.Config) (string, error) {
return fmt.Sprintf("%s/Library/LaunchAgents/%s.plist", homeDir, cfg.Name), nil
}

return "", fmt.Errorf("daemon mode not supported on current OS")
return "", errors.New("daemon mode not supported on current OS")
}
3 changes: 2 additions & 1 deletion pkg/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package workspace

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -276,7 +277,7 @@ func getTarget(targetList []apiclient.ProviderTarget, activeProfileName string)

func processPrompting(apiClient *apiclient.APIClient, workspaceName *string, projects *[]apiclient.CreateProjectDTO, workspaceNames []string, ctx context.Context) error {
if workspace_util.CheckAnyProjectConfigurationFlagSet(projectConfigurationFlags) {
return fmt.Errorf("please provide the repository URL in order to set up custom project details through the CLI")
return errors.New("please provide the repository URL in order to set up custom project details through the CLI")
}

gitProviders, res, err := apiClient.GitProviderAPI.ListGitProviders(ctx).Execute()
Expand Down
11 changes: 6 additions & 5 deletions pkg/docker/create_devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (d *DockerClient) CreateFromDevcontainer(opts CreateDevcontainerOptions) (s

workspaceFolder := config.Workspace.WorkspaceFolder
if workspaceFolder == "" {
return "", "", fmt.Errorf("unable to determine workspace folder from devcontainer configuration")
return "", "", errors.New("unable to determine workspace folder from devcontainer configuration")
}

remoteUser := config.MergedConfiguration.RemoteUser
Expand All @@ -93,7 +94,7 @@ func (d *DockerClient) CreateFromDevcontainer(opts CreateDevcontainerOptions) (s

devcontainerConfig, ok := mergedConfig["configuration"].(map[string]interface{})
if !ok {
return "", "", fmt.Errorf("unable to find devcontainer configuration in merged configuration")
return "", "", errors.New("unable to find devcontainer configuration in merged configuration")
}

envVars := map[string]string{}
Expand Down Expand Up @@ -252,7 +253,7 @@ func (d *DockerClient) CreateFromDevcontainer(opts CreateDevcontainerOptions) (s

resultIndex := strings.LastIndex(output, "{")
if resultIndex == -1 {
return "", "", fmt.Errorf("unable to find result in devcontainer output")
return "", "", errors.New("unable to find result in devcontainer output")
}

resultRaw := output[resultIndex:]
Expand Down Expand Up @@ -365,7 +366,7 @@ func (d *DockerClient) readDevcontainerConfig(opts *CreateDevcontainerOptions, p

configStartIndex := strings.Index(output, "{")
if configStartIndex == -1 {
return "", nil, fmt.Errorf("unable to find start of JSON in devcontainer configuration")
return "", nil, errors.New("unable to find start of JSON in devcontainer configuration")
}

rawConfig := output[configStartIndex:]
Expand Down Expand Up @@ -542,7 +543,7 @@ func (d *DockerClient) getRemoteComposeContent(opts *CreateDevcontainerOptions,

nameIndex := strings.Index(output, "name: ")
if nameIndex == -1 {
return "", fmt.Errorf("unable to find service name in compose config")
return "", errors.New("unable to find service name in compose config")
}

return output[nameIndex:], nil
Expand Down
5 changes: 3 additions & 2 deletions pkg/gitprovider/aws-codecommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gitprovider

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -102,7 +103,7 @@ func (g *AwsCodeCommitGitProvider) GetUrlFromContext(repoContext *GetRepositoryC
func (g *AwsCodeCommitGitProvider) getApiClient() (*codecommit.Client, error) {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, fmt.Errorf("failed to load AWS SDK configuration")
return nil, errors.New("failed to load AWS SDK configuration")
}
client := codecommit.NewFromConfig(cfg)

Expand Down Expand Up @@ -180,7 +181,7 @@ func (g *AwsCodeCommitGitProvider) GetUser() (*GitUser, error) {
// No extra configuration is needed for the IAM service API.
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, fmt.Errorf("failed to load AWS SDK configuration")
return nil, errors.New("failed to load AWS SDK configuration")
}
iamclient := iam.NewFromConfig(cfg)
user, err := iamclient.GetUser(context.TODO(), &iam.GetUserInput{})
Expand Down
Loading

0 comments on commit b32b732

Please sign in to comment.