Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions packages/api/internal/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@ import (

var tracer = otel.Tracer("github.com/e2b-dev/infra/packages/api/internal/auth")

type AuthorizationHeaderMissingError struct{}

func (e *AuthorizationHeaderMissingError) Error() string {
return "authorization header is missing"
}

var (
ErrNoAuthHeader = &AuthorizationHeaderMissingError{}
ErrNoAuthHeader = errors.New("authorization header is missing")
ErrInvalidAuthHeader = errors.New("authorization header is malformed")
)

Expand Down
3 changes: 2 additions & 1 deletion packages/api/internal/handlers/sandbox_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ func (a *APIStore) DeleteSandboxesSandboxID(

// remove any snapshots when the sandbox is not running
deleteSnapshotErr := a.deleteSnapshot(ctx, sandboxID, teamID, team.ClusterID)
var envNotFoundErr db.EnvNotFoundError
switch {
case errors.Is(deleteSnapshotErr, db.EnvNotFoundError{}):
case errors.As(deleteSnapshotErr, &envNotFoundErr):
case deleteSnapshotErr != nil:
telemetry.ReportError(ctx, "error deleting sandbox", deleteSnapshotErr)
a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error deleting sandbox: %s", deleteSnapshotErr))
Expand Down
3 changes: 2 additions & 1 deletion packages/api/internal/handlers/template_build_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem
}

buildInfo, err := a.templateBuildsCache.Get(ctx, buildUUID, templateID)
var templateBuildNotFoundErr db.TemplateBuildNotFoundError
if err != nil {
if errors.Is(err, db.TemplateBuildNotFoundError{}) {
if errors.As(err, &templateBuildNotFoundErr) {
a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Build '%s' not found", buildUUID))
return
}
Expand Down
6 changes: 4 additions & 2 deletions packages/api/internal/orchestrator/pause_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ func (o *Orchestrator) pauseSandbox(ctx context.Context, node *nodemanager.Node,
}

err = snapshotInstance(ctx, o, node, sbx, envBuild.EnvID, envBuild.ID.String())
if errors.Is(err, PauseQueueExhaustedError{}) {
var pauseQueueErrPauseExhausted PauseQueueExhaustedError
if errors.As(err, &pauseQueueErrPauseExhausted) {
telemetry.ReportCriticalError(ctx, "pause queue exhausted", err)

return PauseQueueExhaustedError{}
}

if err != nil && !errors.Is(err, PauseQueueExhaustedError{}) {
var pauseQueueErr PauseQueueExhaustedError
if err != nil && !errors.As(err, &pauseQueueErr) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Redundant Error Variables in Function

The code declares two distinct variables of type PauseQueueExhaustedError (pauseQueueErrPauseExhausted and pauseQueueErr) for errors.As checks within the same function. This introduces redundant variable declarations and could lead to confusion.

Fix in Cursor Fix in Web

telemetry.ReportCriticalError(ctx, "error pausing sandbox", err)

return fmt.Errorf("error pausing sandbox: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestReservation_Exceeded(t *testing.T) {

_, err := cache.Reserve(sandboxID, teamID, 0)
require.Error(t, err)
assert.IsType(t, &sandbox.LimitExceededError{}, err)
var limitExceededError *sandbox.LimitExceededError
require.ErrorAs(t, err, &limitExceededError)
}

func TestReservation_SameSandbox(t *testing.T) {
Expand All @@ -46,7 +47,8 @@ func TestReservation_SameSandbox(t *testing.T) {

_, err = cache.Reserve(sandboxID, teamID, 10)
require.Error(t, err)
assert.IsType(t, &sandbox.AlreadyBeingStartedError{}, err)
var alreadyBeingStartedErr *sandbox.AlreadyBeingStartedError
require.ErrorAs(t, err, &alreadyBeingStartedErr)
}

func TestReservation_Release(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion packages/orchestrator/internal/sandbox/block/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (c *Chunker) Slice(ctx context.Context, off, length int64) ([]byte, error)
return b, nil
}

if !errors.As(err, &BytesNotAvailableError{}) {
var bytesErr BytesNotAvailableError
if !errors.As(err, &bytesErr) {
timer.End(ctx, length,
attribute.String(result, "failure"),
attribute.String(pullType, pullTypeLocal),
Expand Down
3 changes: 2 additions & 1 deletion packages/orchestrator/internal/sandbox/block/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (o *Overlay) ReadAt(ctx context.Context, p []byte, off int64) (int, error)
continue
}

if !errors.As(err, &BytesNotAvailableError{}) {
var bytesNotAvailableErr BytesNotAvailableError
if !errors.As(err, &bytesNotAvailableErr) {
return n, fmt.Errorf("error reading from cache: %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/orchestrator/internal/sandbox/nbd/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ func (d *DevicePool) ReleaseDeviceWithRetry(idx DeviceSlot) error {
for {
attempt++
err := d.ReleaseDevice(idx)
if errors.Is(err, DeviceInUseError{}) {
var deviceInUseErr DeviceInUseError
if errors.As(err, &deviceInUseErr) {
if attempt%100 == 0 {
zap.L().Error("error releasing device", zap.Int("attempt", attempt), zap.Error(err))
}
Expand Down
21 changes: 6 additions & 15 deletions packages/shared/pkg/proxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ import (
"github.com/e2b-dev/infra/packages/shared/pkg/proxy/template"
)

type InvalidHostError struct{}

func (e *InvalidHostError) Error() string {
return "invalid url host"
}

type InvalidSandboxPortError struct{}

func (e *InvalidSandboxPortError) Error() string {
return "invalid sandbox port"
}
var (
invalidHostErr = errors.New("invalid url host")
invalidPortErr = errors.New("invalid sandbox port")
)

func NewErrSandboxNotFound(sandboxId string) *SandboxNotFoundError {
return &SandboxNotFoundError{
Expand All @@ -43,16 +36,14 @@ func handler(p *pool.ProxyPool, getDestination func(r *http.Request) (*pool.Dest
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
d, err := getDestination(r)

var invalidHostErr *InvalidHostError
if errors.As(err, &invalidHostErr) {
if errors.Is(err, invalidHostErr) {
zap.L().Warn("invalid host", zap.String("host", r.Host))
http.Error(w, "Invalid host", http.StatusBadRequest)

return
}

var invalidPortErr *InvalidSandboxPortError
if errors.As(err, &invalidPortErr) {
if errors.Is(err, invalidPortErr) {
zap.L().Warn("invalid sandbox port", zap.String("host", r.Host))
http.Error(w, "Invalid sandbox port", http.StatusBadRequest)

Expand Down
6 changes: 3 additions & 3 deletions packages/shared/pkg/proxy/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ func ParseHost(host string) (sandboxID string, port uint64, err error) {

// There must be always domain part used
if dot == -1 {
return "", 0, &InvalidHostError{}
return "", 0, invalidHostErr
}

// Keep only the left-most subdomain part, i.e. everything before the
host = host[:dot]

hostParts := strings.Split(host, "-")
if len(hostParts) < 2 {
return "", 0, &InvalidHostError{}
return "", 0, invalidHostErr
}

sandboxPortString := hostParts[0]
sandboxID = hostParts[1]

sandboxPort, err := strconv.ParseUint(sandboxPortString, 10, 64)
if err != nil {
return "", 0, &InvalidSandboxPortError{}
return "", 0, invalidPortErr
}

return sandboxID, sandboxPort, nil
Expand Down
Loading