Moved error types to their respective packages
All checks were successful
/ test (push) Successful in 13s

This commit is contained in:
Eduard Urbach 2025-06-20 12:46:53 +02:00
parent 735246bd38
commit 6dfa7ca00d
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
19 changed files with 134 additions and 118 deletions

View file

@ -1,14 +0,0 @@
package errors
var (
ExpectedFunctionDefinition = &String{"Expected function definition"}
ExpectedPackageName = &String{"Expected package name"}
InvalidFunctionDefinition = &String{"Invalid function definition"}
MissingBlockStart = &String{"Missing '{'"}
MissingBlockEnd = &String{"Missing '}'"}
MissingGroupStart = &String{"Missing '('"}
MissingGroupEnd = &String{"Missing ')'"}
MissingParameter = &String{"Missing parameter"}
MissingType = &String{"Missing type"}
NoInputFiles = &String{"No input files"}
)

View file

@ -2,10 +2,10 @@ package errors
import (
"fmt"
"os"
"path/filepath"
"git.urbach.dev/cli/q/src/fs"
"git.urbach.dev/cli/q/src/global"
"git.urbach.dev/cli/q/src/token"
)
@ -47,13 +47,7 @@ func (e *FileError) LineColumn() (int, int) {
// Path returns the relative path of the file to shorten the error message.
func (e *FileError) Path() string {
cwd, err := os.Getwd()
if err != nil {
return e.file.Path
}
relative, err := filepath.Rel(cwd, e.file.Path)
relative, err := filepath.Rel(global.WorkingDirectory, e.file.Path)
if err != nil {
return e.file.Path

View file

@ -27,6 +27,12 @@ func TestRelativePath(t *testing.T) {
assert.Equal(t, err.Path(), relPath)
}
func TestString(t *testing.T) {
msg := "Static error"
err := errors.String(msg)
assert.Equal(t, err.Error(), msg)
}
func test(t *testing.T, path string) *errors.FileError {
contents, oserr := os.ReadFile(path)
assert.Nil(t, oserr)

View file

@ -1,13 +0,0 @@
package errors
import "fmt"
// InvalidCharacter is created when an invalid character appears.
type InvalidCharacter struct {
Character string
}
// Error implements the error interface.
func (err *InvalidCharacter) Error() string {
return fmt.Sprintf("Invalid character '%s'", err.Character)
}

View file

@ -1,13 +0,0 @@
package errors
import "fmt"
// InvalidTopLevel error is created when a top-level instruction is not valid.
type InvalidTopLevel struct {
Instruction string
}
// Error implements the error interface.
func (err *InvalidTopLevel) Error() string {
return fmt.Sprintf("Invalid top level instruction '%s'", err.Instruction)
}

View file

@ -1,13 +0,0 @@
package errors
import "fmt"
// IsNotDirectory error is created when a path is not a directory.
type IsNotDirectory struct {
Path string
}
// Error implements the error interface.
func (err *IsNotDirectory) Error() string {
return fmt.Sprintf("'%s' is not a directory", err.Path)
}

View file

@ -1,11 +1,6 @@
package errors
// String is used for static errors that have no parameters.
type String struct {
Message string
}
// Error implements the error interface.
func (err *String) Error() string {
return err.Message
// String creates a static error message without parameters.
func String(message string) *static {
return &static{Message: message}
}

11
src/errors/static.go Normal file
View file

@ -0,0 +1,11 @@
package errors
// static is used for static errors that have no parameters.
type static struct {
Message string
}
// Error implements the error interface.
func (err *static) Error() string {
return err.Message
}