Moved error types to their respective packages
All checks were successful
/ test (push) Successful in 13s
All checks were successful
/ test (push) Successful in 13s
This commit is contained in:
parent
735246bd38
commit
6dfa7ca00d
19 changed files with 134 additions and 118 deletions
|
@ -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"}
|
||||
)
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
11
src/errors/static.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue