-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming_errors.go
More file actions
53 lines (46 loc) · 1.56 KB
/
Copy pathstreaming_errors.go
File metadata and controls
53 lines (46 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gottp
import (
"errors"
"fmt"
"strings"
)
// ErrTemplateNotStreamable is returned (wrapped) by ParseStream when the
// template's top-level groups don't all pass the streamability check.
// Use errors.Is(err, ErrTemplateNotStreamable) to match.
var ErrTemplateNotStreamable = errors.New("template is not streamable")
// TemplateNotStreamableError carries the per-group reasons explaining why
// a template failed the streamability check. errors.Is matches against
// ErrTemplateNotStreamable.
type TemplateNotStreamableError struct {
Reasons []string
}
func (e *TemplateNotStreamableError) Error() string {
if len(e.Reasons) == 0 {
return "template is not streamable"
}
return fmt.Sprintf("template is not streamable: %s", strings.Join(e.Reasons, "; "))
}
func (e *TemplateNotStreamableError) Is(target error) bool {
return target == ErrTemplateNotStreamable
}
func (e *TemplateNotStreamableError) Unwrap() error {
return ErrTemplateNotStreamable
}
// WhyNotStreamable reports whether the template is streamable; if not,
// returns one human-readable reason per non-streamable top-level group.
// Useful for template-readiness audits without round-tripping through
// ParseStream + error inspection.
func WhyNotStreamable(c *CompiledTemplate) (streamable bool, reasons []string) {
if c == nil || c.compiled == nil {
return false, []string{"compiled template is nil"}
}
if c.compiled.Streamable {
return true, nil
}
for _, g := range c.compiled.Groups {
if !g.Streamable {
reasons = append(reasons, g.NonStreamableReasons...)
}
}
return false, reasons
}