blob: d8672c1008b72f69719ae5037b8f3b838b4f7224 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001// Copyright 2017 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package compiler
16
17// Error represents compiler errors and their location in the document.
18type Error struct {
19 Context *Context
20 Message string
21}
22
23// NewError creates an Error.
24func NewError(context *Context, message string) *Error {
25 return &Error{Context: context, Message: message}
26}
27
28// Error returns the string value of an Error.
29func (err *Error) Error() string {
30 if err.Context == nil {
31 return "ERROR " + err.Message
32 }
33 return "ERROR " + err.Context.Description() + " " + err.Message
34}
35
36// ErrorGroup is a container for groups of Error values.
37type ErrorGroup struct {
38 Errors []error
39}
40
41// NewErrorGroupOrNil returns a new ErrorGroup for a slice of errors or nil if the slice is empty.
42func NewErrorGroupOrNil(errors []error) error {
43 if len(errors) == 0 {
44 return nil
45 } else if len(errors) == 1 {
46 return errors[0]
47 } else {
48 return &ErrorGroup{Errors: errors}
49 }
50}
51
52func (group *ErrorGroup) Error() string {
53 result := ""
54 for i, err := range group.Errors {
55 if i > 0 {
56 result += "\n"
57 }
58 result += err.Error()
59 }
60 return result
61}