* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package errors
|
|
|
|
// Template is useful constructing Match Err templates. Templates allow you to
|
|
// match Errs without specifying a Code. In other words, just Match using the
|
|
// Errs: Kind, Op, etc.
|
|
type Template struct {
|
|
Err // Err embedded to support matching Errs
|
|
Kind Kind // Kind allows explicit matching on a Template without a Code.
|
|
}
|
|
|
|
// T creates a new Template for matching Errs. Invalid parameters are ignored.
|
|
// If more than is one parameter for a given type, only the last one is used.
|
|
func T(args ...any) *Template {
|
|
t := &Template{}
|
|
for _, a := range args {
|
|
switch arg := a.(type) {
|
|
case Code:
|
|
t.Code = arg
|
|
case string:
|
|
t.Msg = arg
|
|
case Op:
|
|
t.Op = arg
|
|
case *Err: // order is important, this match must before "case error:"
|
|
c := *arg
|
|
t.Wrapped = &c
|
|
case error:
|
|
t.Wrapped = arg
|
|
case Kind:
|
|
t.Kind = arg
|
|
default:
|
|
// ignore it
|
|
}
|
|
}
|
|
return t
|
|
}
|
|
|
|
// Info about the Template, which is useful when matching a Template's Kind with
|
|
// an Err's Kind.
|
|
func (t *Template) Info() Info {
|
|
if t == nil {
|
|
return errorCodeInfo[Unknown]
|
|
}
|
|
switch {
|
|
case t.Code != Unknown:
|
|
return t.Code.Info()
|
|
case t.Kind != Other:
|
|
return Info{
|
|
Message: "Unknown",
|
|
Kind: t.Kind,
|
|
}
|
|
default:
|
|
return errorCodeInfo[Unknown]
|
|
}
|
|
}
|
|
|
|
// Error satisfies the error interface but we intentionally don't return
|
|
// anything of value, in an effort to stop users from substituting Templates in
|
|
// place of Errs, when creating domain errors.
|
|
func (t *Template) Error() string {
|
|
return "Template error"
|
|
}
|
|
|
|
// Match the template against the error. The error must be of type *Err, or wrap an error of type *Err,
|
|
// otherwise match will return false. Matches all non-empty fields of the template against the
|
|
// error.
|
|
func Match(t *Template, err error) bool {
|
|
if t == nil || err == nil {
|
|
return false
|
|
}
|
|
var e *Err
|
|
if !As(err, &e) {
|
|
return false
|
|
}
|
|
|
|
if t.Code != Unknown && t.Code != e.Code {
|
|
return false
|
|
}
|
|
if t.Msg != "" && t.Msg != e.Msg {
|
|
return false
|
|
}
|
|
if t.Op != "" && t.Op != e.Op {
|
|
return false
|
|
}
|
|
if t.Kind != Other && t.Info().Kind != e.Info().Kind {
|
|
return false
|
|
}
|
|
if t.Wrapped != nil {
|
|
if wrappedT, ok := t.Wrapped.(*Template); ok {
|
|
return Match(wrappedT, e.Wrapped)
|
|
}
|
|
if e.Wrapped != nil && t.Wrapped.Error() != e.Wrapped.Error() {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|