mirror of
https://github.com/ollama/ollama.git
synced 2025-05-11 10:26:53 +02:00
add line numbers for parser errors (#7326)
This commit is contained in:
parent
0679d491fe
commit
4efb98cb4f
2 changed files with 68 additions and 9 deletions
|
@ -3,6 +3,7 @@ package parser
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
@ -180,8 +181,15 @@ func TestParseFileBadCommand(t *testing.T) {
|
|||
FROM foo
|
||||
BADCOMMAND param1 value1
|
||||
`
|
||||
parserError := &ParserError{
|
||||
LineNumber: 3,
|
||||
Msg: errInvalidCommand.Error(),
|
||||
}
|
||||
|
||||
_, err := ParseFile(strings.NewReader(input))
|
||||
require.ErrorIs(t, err, errInvalidCommand)
|
||||
if !errors.As(err, &parserError) {
|
||||
t.Errorf("unexpected error: expected: %s, actual: %s", parserError.Error(), err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFileMessages(t *testing.T) {
|
||||
|
@ -245,7 +253,10 @@ FROM foo
|
|||
MESSAGE badguy I'm a bad guy!
|
||||
`,
|
||||
nil,
|
||||
errInvalidMessageRole,
|
||||
&ParserError{
|
||||
LineNumber: 3,
|
||||
Msg: errInvalidMessageRole.Error(),
|
||||
},
|
||||
},
|
||||
{
|
||||
`
|
||||
|
@ -264,13 +275,35 @@ MESSAGE system`,
|
|||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
for _, tt := range cases {
|
||||
t.Run("", func(t *testing.T) {
|
||||
modelfile, err := ParseFile(strings.NewReader(c.input))
|
||||
require.ErrorIs(t, err, c.err)
|
||||
modelfile, err := ParseFile(strings.NewReader(tt.input))
|
||||
|
||||
if modelfile != nil {
|
||||
assert.Equal(t, c.expected, modelfile.Commands)
|
||||
assert.Equal(t, tt.expected, modelfile.Commands)
|
||||
}
|
||||
|
||||
if tt.err == nil {
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, but got %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
switch tt.err.(type) {
|
||||
case *ParserError:
|
||||
var pErr *ParserError
|
||||
if errors.As(err, &pErr) {
|
||||
// got the correct type of error
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if errors.Is(err, tt.err) {
|
||||
return
|
||||
}
|
||||
|
||||
t.Fatalf("unexpected error: expected: %v, actual: %v", tt.err, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue