Skip to content

Commit

Permalink
feat: introduce error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
abuchanan-airbyte committed Sep 18, 2024
1 parent d70ad70 commit 9a159db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/cmd/local/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func (e ContainerNotRunningError) Error() string {
return fmt.Sprintf("container %q is not running (status = %q)", e.Container, e.Status)
}

func (e ContainerNotRunningError) ErrorCode() string {
return "ContainerNotRunning"
}

type InvalidPortError struct {
Port string
Inner error
Expand All @@ -162,6 +166,7 @@ type InvalidPortError struct {
func (e InvalidPortError) Unwrap() error {
return e.Inner
}

func (e InvalidPortError) Error() string {
return fmt.Sprintf("unable to convert host port %s to integer: %s", e.Port, e.Inner)
}
11 changes: 11 additions & 0 deletions internal/cmd/local/localerr/localerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var _ error = (*LocalError)(nil)
type LocalError struct {
help string
msg string
code string
}

// Help will displayed to the user if this specific error is ever returned.
Expand All @@ -18,23 +19,30 @@ func (e *LocalError) Error() string {
return e.msg
}

func (e *LocalError) ErrorCode() string {
return e.code
}

var (
// ErrAirbyteDir is returned anytime an there is an issue in accessing the paths.Airbyte directory.
ErrAirbyteDir = &LocalError{
code: "ErrAirbyteDir",
msg: "airbyte directory is inaccessible",
help: `The ~/.airbyte directory is inaccessible.
You may need to remove this directory before trying your command again.`,
}

// ErrClusterNotFound is returned in the event that no cluster was located.
ErrClusterNotFound = &LocalError{
code: "ErrClusterNotFound",
msg: "no existing cluster found",
help: `No cluster was found. If this is unexpected,
you may need to run the "local install" command again.`,
}

// ErrDocker is returned anytime an error occurs when attempting to communicate with docker.
ErrDocker = &LocalError{
code: "ErrDocker",
msg: "error communicating with docker",
help: `An error occurred while communicating with the Docker daemon.
Ensure that Docker is running and is accessible. You may need to upgrade to a newer version of Docker.
Expand All @@ -43,6 +51,7 @@ For additional help please visit https://docs.docker.com/get-docker/`,

// ErrKubernetes is returned anytime an error occurs when attempting to communicate with the kubernetes cluster.
ErrKubernetes = &LocalError{
code: "ErrKubernetes",
msg: "error communicating with kubernetes",
help: `An error occurred while communicating with the Kubernetes cluster.
If this error persists, you may need to run the uninstall command before attempting to run
Expand All @@ -51,6 +60,7 @@ For additional help please visit https://docs.docker.com/get-docker/`,

// ErrIngress is returned in the event that ingress configuration failed.
ErrIngress = &LocalError{
code: "ErrIngress",
msg: "error configuring ingress",
help: `An error occurred while configuring ingress.
This could be in indication that the ingress port is already in use by a different application.
Expand All @@ -59,6 +69,7 @@ The ingress port can be changed by passing the flag --port.`,

// ErrPort is returned in the event that the requested port is unavailable.
ErrPort = &LocalError{
code: "ErrPort",
msg: "error verifying port availability",
help: `An error occurred while verifying if the request port is available.
This could be in indication that the ingress port is already in use by a different application.
Expand Down
12 changes: 12 additions & 0 deletions internal/telemetry/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telemetry
import (
"bytes"
"context"
"errors"
"fmt"
"maps"
"net/http"
Expand Down Expand Up @@ -135,6 +136,7 @@ func (s *SegmentClient) send(ctx context.Context, es EventState, et EventType, e

if ee != nil {
properties["error"] = ee.Error()
findErrorCode(properties, ee)
}

body := body{
Expand Down Expand Up @@ -172,3 +174,13 @@ type body struct {
Timestamp string `json:"timestamp"`
WriteKey string `json:"writeKey"`
}

func findErrorCode(properties map[string]string, err error) {
if v, ok := err.(interface{ErrorCode() string}); ok {
properties["error_code"] = v.ErrorCode()
return
}
if sub := errors.Unwrap(err); sub != nil {
findErrorCode(properties, sub)
}
}

0 comments on commit 9a159db

Please sign in to comment.