mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-28 03:05:04 -06:00
f1b9c7586b
Previously if the errors passed into the presentation Output were not structured w/ JSON tags for marshaling the error would be an empty string. This changes to wrap the errors with a struct in cases where they would otherwise not be formatted. We do this by forcing every error into a structure and translating known error types into it. Fixes: #1726 Fixes: #1724 Signed-off-by: Patrick East <east.patrick@gmail.com>
76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
// Copyright 2017 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package loader
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/open-policy-agent/opa/ast"
|
|
)
|
|
|
|
// Errors is a wrapper for multiple loader errors.
|
|
type Errors []error
|
|
|
|
func (e Errors) Error() string {
|
|
if len(e) == 0 {
|
|
return "no error(s)"
|
|
}
|
|
if len(e) == 1 {
|
|
return "1 error occurred during loading: " + e[0].Error()
|
|
}
|
|
buf := make([]string, len(e))
|
|
for i := range buf {
|
|
buf[i] = e[i].Error()
|
|
}
|
|
return fmt.Sprintf("%v errors occurred during loading:\n", len(e)) + strings.Join(buf, "\n")
|
|
}
|
|
|
|
func (e *Errors) add(err error) {
|
|
if errs, ok := err.(ast.Errors); ok {
|
|
for i := range errs {
|
|
*e = append(*e, errs[i])
|
|
}
|
|
} else {
|
|
*e = append(*e, err)
|
|
}
|
|
}
|
|
|
|
func newResult() *Result {
|
|
return &Result{
|
|
Documents: map[string]interface{}{},
|
|
Modules: map[string]*RegoFile{},
|
|
}
|
|
}
|
|
|
|
type unsupportedDocumentType string
|
|
|
|
func (path unsupportedDocumentType) Error() string {
|
|
return string(path) + ": bad document type"
|
|
}
|
|
|
|
type unrecognizedFile string
|
|
|
|
func (path unrecognizedFile) Error() string {
|
|
return string(path) + ": can't recognize file type"
|
|
}
|
|
|
|
func isUnrecognizedFile(err error) bool {
|
|
_, ok := err.(unrecognizedFile)
|
|
return ok
|
|
}
|
|
|
|
type mergeError string
|
|
|
|
func (e mergeError) Error() string {
|
|
return string(e) + ": merge error"
|
|
}
|
|
|
|
type emptyModuleError string
|
|
|
|
func (e emptyModuleError) Error() string {
|
|
return string(e) + ": empty policy"
|
|
}
|