Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
internal: update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aavshr committed Sep 15, 2023
1 parent 165bf1f commit 244f319
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 66 deletions.
Binary file added internal/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func StoreProjectKey(projectId string, projectKey string) error {
}

keysFilePath := filepath.Join(home, spaceProjectKeysPath)
err = ioutil.WriteFile(keysFilePath, marshalled, 0660)
err = os.WriteFile(keysFilePath, marshalled, 0660)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions internal/discovery/screenshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

var (
// ErrInvalidScreenshotPath cannot find screenshot path
ErrInvalidScreenshotPath = errors.New("invalid screenshot path")
// ErrInvalidMediaPath cannot find media path
ErrInvalidMediaPath = errors.New("invalid media path")
)

// Screenshot xx
Expand All @@ -31,7 +31,7 @@ func ParseScreenshot(paths []string) ([]Screenshot, error) {
} else {
absPath, err := filepath.Abs(path)
if err != nil {
return screenshots, ErrInvalidScreenshotPath
return screenshots, ErrInvalidMediaPath
}

isdir, err := isDir(&absPath)
Expand All @@ -56,7 +56,7 @@ func ParseScreenshot(paths []string) ([]Screenshot, error) {

file, err := os.Open(absPath)
if err != nil {
return screenshots, ErrInvalidScreenshotPath
return screenshots, ErrInvalidMediaPath
}
defer file.Close()
content, err := io.ReadAll(file)
Expand All @@ -80,7 +80,7 @@ func getFilesInDirectory(directoryPath string) ([]string, error) {

files, err := os.ReadDir(directoryPath)
if err != nil {
return nil, ErrInvalidScreenshotPath
return nil, ErrInvalidMediaPath
}

for _, file := range files {
Expand All @@ -98,7 +98,7 @@ func getFilesInDirectory(directoryPath string) ([]string, error) {
func isDir(path *string) (bool, error) {
fileInfo, err := os.Stat(*path)
if err != nil {
return false, ErrInvalidScreenshotPath
return false, ErrInvalidMediaPath
}

if fileInfo.IsDir() {
Expand Down
Binary file added internal/spacefile/.DS_Store
Binary file not shown.
77 changes: 18 additions & 59 deletions internal/spacefile/spacefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand All @@ -13,9 +12,6 @@ import (

_ "embed"

"github.com/deta/space/pkg/components/emoji"
"github.com/deta/space/pkg/components/styles"
"github.com/deta/space/pkg/util/fs"
"github.com/deta/space/shared"
"github.com/santhosh-tekuri/jsonschema/v5"
"gopkg.in/yaml.v3"
Expand All @@ -28,11 +24,10 @@ const (

//go:embed schemas/spacefile.schema.json
var spacefileSchemaString string
var spacefileSchema *jsonschema.Schema = jsonschema.MustCompileString("", spacefileSchemaString)
var spacefileSchema = jsonschema.MustCompileString("", spacefileSchemaString)

var (
ErrSpacefileNotFound = errors.New("Spacefile not found")
ErrInvalidSpacefile = errors.New("unable to parse Spacefile")
ErrDuplicateMicros = errors.New("micro names have to be unique")
ErrMultiplePrimary = errors.New("multiple primary micros present")
ErrNoPrimaryMicro = errors.New("no primary micro present")
Expand Down Expand Up @@ -137,14 +132,14 @@ func extractApiKey(v any, microIndex int, apiKeyIndex int) (map[string]any, bool
}

var (
microReg = regexp.MustCompile(`\/micros\/(\d+)$`)
actionReg = regexp.MustCompile(`\/micros\/(\d+)\/actions\/(\d+)$`)
commandsReg = regexp.MustCompile(`\/micros\/(\d+)\/commands$`)
includeReg = regexp.MustCompile(`\/micros\/(\d+)\/include$`)
publicRoutesReg = regexp.MustCompile(`\/micros\/(\d+)\/public_routes$`)
presetsReg = regexp.MustCompile(`\/micros\/(\d+)\/presets$`)
envReg = regexp.MustCompile(`\/micros\/(\d+)\/presets\/env\/(\d+)$`)
apiKeyReg = regexp.MustCompile(`\/micros\/(\d+)\/presets\/api_keys\/(\d+)$`)
microReg = regexp.MustCompile(`/micros/(\d+)$`)
actionReg = regexp.MustCompile(`/micros/(\d+)/actions/(\d+)$`)
commandsReg = regexp.MustCompile(`/micros/(\d+)/commands$`)
includeReg = regexp.MustCompile(`/micros/(\d+)/include$`)
publicRoutesReg = regexp.MustCompile(`/micros/(\d+)/public_routes$`)
presetsReg = regexp.MustCompile(`/micros/(\d+)/presets$`)
envReg = regexp.MustCompile(`/micros/(\d+)/presets/env/(\d+)$`)
apiKeyReg = regexp.MustCompile(`/micros/(\d+)/presets/api_keys/(\d+)$`)
numberReg = regexp.MustCompile(`^\d+$`)
)

Expand Down Expand Up @@ -250,14 +245,14 @@ func LoadSpacefile(projectDir string) (*Spacefile, error) {
}

// read raw contents from spacefile file
content, err := ioutil.ReadFile(filepath.Join(spacefilePath))
content, err := os.ReadFile(filepath.Join(spacefilePath))
if err != nil {
return nil, fmt.Errorf("failed to read contents of spacefile file: %w", err)
return nil, fmt.Errorf("failed to read, %w", err)
}

var v any
if err := yaml.Unmarshal(content, &v); err != nil {
return nil, ErrInvalidSpacefile
return nil, err
}

// validate against schema
Expand All @@ -270,14 +265,14 @@ func LoadSpacefile(projectDir string) (*Spacefile, error) {

var spacefile Spacefile
if err := yaml.Unmarshal(content, &spacefile); err != nil {
return nil, ErrInvalidSpacefile
return nil, err
}

foundPrimaryMicro := false
micros := make(map[string]struct{})
for i, micro := range spacefile.Micros {
if _, ok := micros[micro.Name]; ok {
return nil, ErrDuplicateMicros
return nil, fmt.Errorf("%w, duplicate micro `%s` found", ErrDuplicateMicros, micro.Name)
}
micros[micro.Name] = struct{}{}

Expand Down Expand Up @@ -320,29 +315,6 @@ func LoadSpacefile(projectDir string) (*Spacefile, error) {
return &spacefile, nil
}

// OpenRaw returns the raw spacefile file content from sourceDir if it exists
func OpenRaw(sourceDir string) ([]byte, error) {
var exists bool
var err error

exists, err = fs.FileExists(sourceDir, SpacefileName)
if err != nil {
return nil, err
}

if !exists {
return nil, ErrSpacefileNotFound
}

// read raw contents from spacefile file
c, err := ioutil.ReadFile(filepath.Join(sourceDir, SpacefileName))
if err != nil {
return nil, fmt.Errorf("failed to read contents of spacefile file: %w", err)
}

return c, nil
}

func (s *Spacefile) Save(sourceDir string) error {

spacefileDocsUrl := "# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0\n"
Expand All @@ -360,9 +332,9 @@ func (s *Spacefile) Save(sourceDir string) error {
c = append(c, rawSpacefile.Bytes()...)

// write spacefile object to file
err = ioutil.WriteFile(filepath.Join(sourceDir, SpacefileName), c, 0644)
err = os.WriteFile(filepath.Join(sourceDir, SpacefileName), c, 0644)
if err != nil {
return fmt.Errorf("failed to write spacefile object: %w", err)
return fmt.Errorf("failed to write, %w", err)
}

return nil
Expand Down Expand Up @@ -425,7 +397,7 @@ func CreateSpacefileWithMicros(sourceDir string, micros []*shared.Micro) (*Space

err := s.Save(sourceDir)
if err != nil {
return nil, fmt.Errorf("failed to create spacefile with micros in %s, %w", sourceDir, err)
return nil, fmt.Errorf("failed to create Spacefile with micros in %s, %w", sourceDir, err)
}

return s, nil
Expand All @@ -436,7 +408,7 @@ func CreateBlankSpacefile(sourceDir string) (*Spacefile, error) {

err := s.Save(sourceDir)
if err != nil {
return nil, fmt.Errorf("failed to create a blank spacefile in %s, %w", sourceDir, err)
return nil, fmt.Errorf("failed to create a blank Spacefile in %s, %w", sourceDir, err)
}

return s, nil
Expand All @@ -450,16 +422,3 @@ func (s *Spacefile) HasMicro(otherMicro *shared.Micro) bool {
}
return false
}

func ParseSpacefileUnmarshallTypeError(err *yaml.TypeError) string {
errMsg := styles.Errorf("%sError: failed to parse your Spacefile, please make sure you use the correct syntax:", emoji.ErrorExclamation)
for _, err := range err.Errors {
fieldNotValidMatches := regexp.MustCompile(`(?m)(line \d:) field\s(\w+)\snot found in type.*`).FindStringSubmatch(err)
if len(fieldNotValidMatches) > 0 {
errMsg += fmt.Sprintf("\n L %v \"%v\" is not a valid field\n", fieldNotValidMatches[1], fieldNotValidMatches[2])
} else {
errMsg += styles.Boldf("\n L %v\n", err)
}
}
return errMsg
}

0 comments on commit 244f319

Please sign in to comment.