blob: e22035ef6e0ba81ef75b853e1df98062be913661 [file] [log] [blame]
Joe Tsai9834a7d2018-08-01 13:16:49 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package errors implements functions to manipulate errors.
6package errors
7
8import (
9 "fmt"
Joe Tsai9834a7d2018-08-01 13:16:49 -070010)
11
Joe Tsai9834a7d2018-08-01 13:16:49 -070012// New formats a string according to the format specifier and arguments and
13// returns an error that has a "proto" prefix.
14func New(f string, x ...interface{}) error {
15 for i := 0; i < len(x); i++ {
Herbie Onge09920e2019-02-27 13:37:06 -080016 if e, ok := x[i].(*prefixError); ok {
Joe Tsai9834a7d2018-08-01 13:16:49 -070017 x[i] = e.s // avoid "proto: " prefix when chaining
18 }
19 }
20 return &prefixError{s: fmt.Sprintf(f, x...)}
21}
22
23type prefixError struct{ s string }
24
25func (e *prefixError) Error() string { return "proto: " + e.s }
Damien Neil8c86fc52019-06-19 09:28:29 -070026
27func InvalidUTF8(name string) error {
28 return New("field %v contains invalid UTF-8", name)
29}
30
31func RequiredNotSet(name string) error {
32 return New("required field %v not set", name)
33}