1// Copyright 2019 The SwiftShader Authors. 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 15// Package cause provides functions for building wrapped errors. 16package cause 17 18import ( 19 "fmt" 20 "strings" 21) 22 23// Wrap returns a new error wrapping cause with the additional message. 24func Wrap(cause error, msg string, args ...interface{}) error { 25 s := fmt.Sprintf(msg, args...) 26 return fmt.Errorf("%v. Cause: %w", s, cause) 27} 28 29// Merge merges all the errors into a single newline delimited error. 30func Merge(errs ...error) error { 31 if len(errs) == 0 { 32 return nil 33 } 34 strs := make([]string, len(errs)) 35 for i, err := range errs { 36 strs[i] = err.Error() 37 } 38 return fmt.Errorf("%v", strings.Join(strs, "\n")) 39} 40