本文整理汇总了Golang中github.com/go-errors/errors.Errorf函数的典型用法代码示例。如果您正苦于以下问题:Golang Errorf函数的具体用法?Golang Errorf怎么用?Golang Errorf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Errorf函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Clear
/*
Clear will delete things.
If toDelete is empty, EVERYTHING will be deleted.
If toDelete has one element, that index will be deleted.
If toDelete has two elements, that index and doc type will be deleted.
*/
func Clear(c ElasticConnector, toDelete ...string) (err error) {
url := c.GetElasticService()
if len(toDelete) > 2 {
err = errors.Errorf("Can only give at most 2 string args to Clear")
return
} else if len(toDelete) == 2 {
url += fmt.Sprintf("/%v/%v", processIndexName(toDelete[0]), toDelete[1])
} else if len(toDelete) == 1 {
url += fmt.Sprintf("/%v", processIndexName(toDelete[0]))
} else {
url += "/_all"
}
request, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return
}
if c.GetElasticUsername() != "" {
request.SetBasicAuth(c.GetElasticUsername(), c.GetElasticPassword())
}
response, err := c.Client().Do(request)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
err = errors.Errorf("Bad status trying to delete from elasticsearch %v: %v", url, response.Status)
return
}
return
}
开发者ID:DaviWei,项目名称:utils-1,代码行数:38,代码来源:elasticsearch.go
示例2: ErrorfWithLogger
func ErrorfWithLogger(logger Logger, format string, a ...interface{}) *errors.Error {
if logger.Name() == "machine" {
logger.Fprintln(os.Stderr, constants.LOG_LEVEL_ERROR, format, a...)
return errors.Errorf("")
}
return errors.Errorf(Format(format, a...))
}
开发者ID:me-no-dev,项目名称:arduino-builder,代码行数:7,代码来源:errors.go
示例3: AddToIndex
/*
AddToIndex adds source to a search index.
Source must have a field `Id *datastore.key`.
*/
func AddToIndex(c ElasticConnector, index string, source interface{}) (err error) {
sourceVal := reflect.ValueOf(source)
if sourceVal.Kind() != reflect.Ptr {
err = errors.Errorf("%#v is not a pointer", source)
return
}
if sourceVal.Elem().Kind() != reflect.Struct {
err = errors.Errorf("%#v is not a pointer to a struct", source)
return
}
index = processIndexName(index)
value := reflect.ValueOf(source).Elem()
id := value.FieldByName("Id").Interface().(key.Key).Encode()
name := value.Type().Name()
json, err := json.Marshal(source)
if err != nil {
return
}
url := fmt.Sprintf("%s/%s/%s/%s",
c.GetElasticService(),
index,
name,
id)
updatedAtField := value.FieldByName("UpdatedAt")
if updatedAtField.IsValid() {
updatedAtUnixNano := updatedAtField.MethodByName("UnixNano")
if updatedAtUnixNano.IsValid() {
if unixNano, ok := updatedAtUnixNano.Call(nil)[0].Interface().(int64); ok && unixNano > 0 {
millis := unixNano / 1000000
url = fmt.Sprintf("%v?version_type=external_gte&version=%v", url, millis)
}
}
}
request, err := http.NewRequest("PUT", url, bytes.NewBuffer(json))
if err != nil {
return
}
if c.GetElasticUsername() != "" {
request.SetBasicAuth(c.GetElasticUsername(), c.GetElasticPassword())
}
response, err := c.Client().Do(request)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusCreated && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusConflict {
body, _ := ioutil.ReadAll(response.Body)
err = errors.Errorf("Bad status code from elasticsearch %v: %v, %v", url, response.Status, string(body))
return
}
return
}
开发者ID:DaviWei,项目名称:utils-1,代码行数:64,代码来源:elasticsearch.go
示例4: GenerateFlags
/*
GenerateFlags will generate command line flags matching the fields of the provided interface (being a struct pointer).
Any fields tagged with `flag` will be named like the value of the `flag` tag.
*/
func GenerateFlags(i interface{}) (result []string, err error) {
v := reflect.ValueOf(i)
t := v.Type()
if t.Kind() != reflect.Ptr {
err = errors.Errorf("Unable to ParseFlags into %v, it is not a pointer to a struct", v)
return
}
t = t.Elem()
v = v.Elem()
if t.Kind() != reflect.Struct {
err = errors.Errorf("Unable to ParseFlags into %v, it is not a pointer to a struct", v)
return
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
flagName := f.Name
if explicitFlagName := f.Tag.Get("flag"); explicitFlagName != "" {
flagName = explicitFlagName
}
result = append(result, fmt.Sprintf("-%v=%v", flagName, v.Field(i).Interface()))
}
return
}
开发者ID:DaviWei,项目名称:utils-1,代码行数:33,代码来源:utils.go
示例5: Validate
// Validate : Validates a token and returns its signature or an error if the token is not valid.
func (j *JWTEnigma) Validate(token string) (string, error) {
split := strings.Split(token, ".")
if len(split) != 3 {
return "", errors.New("Header, body and signature must all be set")
}
// Parse the token.
parsedToken, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
return nil, errors.Errorf("Unexpected signing method: %v", t.Header["alg"])
}
return jwt.ParseRSAPublicKeyFromPEM(j.PublicKey)
})
if err != nil {
return "", errors.Errorf("Couldn't parse token: %v", err)
} else if !parsedToken.Valid {
return "", errors.Errorf("Token is invalid")
}
// make sure we can work with the data
claimsContext := jwthelper.ClaimsContext(parsedToken.Claims)
if claimsContext.AssertExpired() {
parsedToken.Valid = false
return "", errors.Errorf("Token expired at %v", claimsContext.GetExpiresAt())
}
if claimsContext.AssertNotYetValid() {
parsedToken.Valid = false
return "", errors.Errorf("Token validates in the future: %v", claimsContext.GetNotBefore())
}
return split[2], nil
}
开发者ID:jesseward,项目名称:fosite,代码行数:36,代码来源:jwt.go
示例6: FetchSession
func (d *google) FetchSession(code string) (Session, error) {
conf := *d.conf
token, err := conf.Exchange(oauth2.NoContext, code)
if err != nil {
return nil, err
}
if !token.Valid() {
return nil, errors.Errorf("Token is not valid: %v", token)
}
c := conf.Client(oauth2.NoContext, token)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", d.api, "plus/v1/people/me"), nil)
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("Could not fetch account data because %s", err)
}
var profile map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&profile); err != nil {
return nil, errors.Errorf("Could not validate id token because %s", err)
}
return &DefaultSession{
RemoteSubject: fmt.Sprintf("%s", profile["id"]),
Extra: profile,
}, nil
}
开发者ID:bagocius,项目名称:hydra-1,代码行数:33,代码来源:google.go
示例7: isValidAuthenticationRequest
func isValidAuthenticationRequest(c *HTTPClient, token string, retry bool) (bool, error) {
data := url.Values{}
data.Set("token", token)
request := gorequest.New()
resp, body, errs := request.Post(pkg.JoinURL(c.ep, "/oauth2/introspect")).Type("form").SetBasicAuth(c.clientConfig.ClientID, c.clientConfig.ClientSecret).SendString(data.Encode()).End()
if len(errs) > 0 {
return false, errors.Errorf("Got errors: %v", errs)
} else if resp.StatusCode != http.StatusOK {
return false, errors.Errorf("Status code %d is not 200: %s", resp.StatusCode, body)
}
if retry && resp.StatusCode == http.StatusUnauthorized {
var err error
if c.clientToken, err = c.clientConfig.Token(oauth2.NoContext); err != nil {
return false, errors.New(err)
} else if c.clientToken == nil {
return false, errors.New("Access token could not be retrieved")
}
return isValidAuthenticationRequest(c, token, false)
} else if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("Status code %d is not 200", resp.StatusCode)
}
var introspect struct {
Active bool `json:"active"`
}
if err := json.Unmarshal([]byte(body), &introspect); err != nil {
return false, err
} else if !introspect.Active {
return false, errors.New("Authentication denied")
}
return introspect.Active, nil
}
开发者ID:emmanuel,项目名称:hydra,代码行数:34,代码来源:client.go
示例8: isValidAuthorizeRequest
func isValidAuthorizeRequest(c *HTTPClient, ar *AuthorizeRequest, retry bool) (bool, error) {
request := gorequest.New()
resp, body, errs := request.Post(pkg.JoinURL(c.ep, "/guard/allowed")).SetBasicAuth(c.clientConfig.ClientID, c.clientConfig.ClientSecret).Set("Content-Type", "application/json").Send(*ar).End()
if len(errs) > 0 {
return false, errors.Errorf("Got errors: %v", errs)
} else if retry && resp.StatusCode == http.StatusUnauthorized {
var err error
if c.clientToken, err = c.clientConfig.Token(oauth2.NoContext); err != nil {
return false, errors.New(err)
} else if c.clientToken == nil {
return false, errors.New("Access token could not be retrieved")
}
return isValidAuthorizeRequest(c, ar, false)
} else if resp.StatusCode != http.StatusOK {
return false, errors.Errorf("Status code %d is not 200: %s", resp.StatusCode, body)
}
if err := json.Unmarshal([]byte(body), &isAllowed); err != nil {
return false, errors.Errorf("Could not unmarshall body because %s", err.Error())
}
if !isAllowed.Allowed {
return false, errors.New("Authroization denied")
}
return isAllowed.Allowed, nil
}
开发者ID:emmanuel,项目名称:hydra,代码行数:26,代码来源:client.go
示例9: FetchSession
func (d *dropbox) FetchSession(code string) (Session, error) {
conf := *d.conf
token, err := conf.Exchange(oauth2.NoContext, code)
if err != nil {
return nil, err
}
if !token.Valid() {
return nil, errors.Errorf("Token is not valid: %v", token)
}
c := conf.Client(oauth2.NoContext, token)
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", d.api, "users/get_current_account"), nil)
response, err := c.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, errors.Errorf("Could not fetch account data because %s", err)
}
var acc map[string]interface{}
if err = json.NewDecoder(response.Body).Decode(&acc); err != nil {
return nil, err
}
return &DefaultSession{
RemoteSubject: fmt.Sprintf("%s", acc["account_id"]),
Extra: acc,
}, nil
}
开发者ID:bagocius,项目名称:hydra-1,代码行数:33,代码来源:dropbox.go
示例10: VerifyToken
// Verify a token and output the claims.
func (j *JWT) VerifyToken(tokenData []byte) (*jwt.Token, error) {
// trim possible whitespace from token
tokenData = regexp.MustCompile(`\s*$`).ReplaceAll(tokenData, []byte{})
// Parse the token. Load the key from command line option
token, err := jwt.Parse(string(tokenData), func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
return nil, errors.Errorf("Unexpected signing method: %v", t.Header["alg"])
}
return jwt.ParseRSAPublicKeyFromPEM(j.publicKey)
})
if err != nil {
return nil, errors.Errorf("Couldn't parse token: %v", err)
} else if !token.Valid {
return nil, errors.Errorf("Token is invalid")
}
claims := ClaimsCarrier(token.Claims)
if claims.AssertExpired() {
token.Valid = false
return token, errors.Errorf("Token expired: %v", claims.GetExpiresAt())
}
if claims.AssertNotYetValid() {
token.Valid = false
return token, errors.Errorf("Token is not valid yet: %v", claims.GetNotBefore())
}
return token, nil
}
开发者ID:thanzen,项目名称:hydra,代码行数:29,代码来源:jwt.go
示例11: FetchSession
func (d *signin) FetchSession(code string) (Session, error) {
request := gorequest.New()
// FIXME does not work if redirect contains query params
resp, body, errs := request.Get(fmt.Sprintf("%s?verify=%s", d.login, code)).End()
if len(errs) > 0 {
return nil, errors.Errorf("Could not exchange code: %s", errs)
} else if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("Could not exchange code, received status: %d", resp.StatusCode)
}
var p payload
if err := json.Unmarshal([]byte(body), &p); err != nil {
return nil, errors.Errorf("Could not parse answer: %v", err)
}
if p.Subject == "" {
return nil, errors.Errorf("Field subject is empty, got %s", body)
}
return &DefaultSession{
ForceLocalSubject: p.Subject,
Extra: map[string]interface{}{},
}, nil
}
开发者ID:bagocius,项目名称:hydra-1,代码行数:25,代码来源:signin.go
示例12: Submit
// Submit sends the provided envelope to stellar-core and parses the response into
// a SubmissionResult
func (sub *submitter) Submit(ctx context.Context, env string) (result SubmissionResult) {
start := time.Now()
defer func() { result.Duration = time.Since(start) }()
// construct the request
u, err := url.Parse(sub.coreURL)
if err != nil {
result.Err = errors.Wrap(err, 1)
return
}
u.Path = "/tx"
q := u.Query()
q.Add("blob", env)
u.RawQuery = q.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
result.Err = errors.Wrap(err, 1)
return
}
// perform the submission
resp, err := sub.http.Do(req)
if err != nil {
result.Err = errors.Wrap(err, 1)
return
}
defer resp.Body.Close()
// parse response
var cresp coreSubmissionResponse
err = json.NewDecoder(resp.Body).Decode(&cresp)
if err != nil {
result.Err = errors.Wrap(err, 1)
return
}
// interpet response
if cresp.Exception != "" {
result.Err = errors.Errorf("stellar-core exception: %s", cresp.Exception)
return
}
switch cresp.Status {
case StatusError:
result.Err = &FailedTransactionError{cresp.Error}
case StatusPending, StatusDuplicate:
//noop. A nil Err indicates success
default:
result.Err = errors.Errorf("Unrecognized stellar-core status response: %s", cresp.Status)
}
return
}
开发者ID:FihlaTV,项目名称:horizon,代码行数:57,代码来源:submitter.go
示例13: DelAll
func DelAll(c PersistenceContext, src interface{}) (err error) {
srcTyp := reflect.TypeOf(src)
if srcTyp.Kind() != reflect.Ptr {
err = errors.Errorf("%+v is not a pointer", src)
return
}
if srcTyp.Elem().Kind() != reflect.Struct {
err = errors.Errorf("%+v is not a pointer to a struct", src)
return
}
return DelQuery(c, src, datastore.NewQuery(reflect.TypeOf(src).Elem().Name()))
}
开发者ID:DaviWei,项目名称:utils-1,代码行数:12,代码来源:gae.go
示例14: checkManifest
func checkManifest() (string, string, error) {
manifest := os.Getenv("WERCKER_DISTELLI_MANIFEST")
if manifest == "" {
return "", "", errors.Errorf("manifest must be set")
}
if _, err := os.Stat(manifest); err != nil {
return "", "", errors.Errorf("manifest file %s not found", manifest)
}
dirname, basename := path.Split(manifest)
return dirname, basename, nil
}
开发者ID:Distelli,项目名称:wercker-step-distelli,代码行数:13,代码来源:main.go
示例15: readVersionHeader
func (f *RrdRawFile) readVersionHeader(reader *RawDataReader) error {
if cookie, err := reader.ReadCString(4); err != nil {
return err
} else if cookie != rrdCookie {
return errors.Errorf("Invalid cookie: %+v", cookie)
}
versionStr, err := reader.ReadCString(5)
if err != nil {
return err
}
version, err := strconv.ParseInt(string(versionStr[:4]), 10, 16)
if err != nil {
return errors.Errorf("Invalid version: %+v", version)
} else if version < 3 {
return errors.Errorf("Version %d not supported: ", version)
}
if floatCookie, err := reader.ReadDouble(); err != nil {
return err
} else if floatCookie != rrdFloatCookie {
return errors.Errorf("Float cookie does not match: %+v != %+v", floatCookie, rrdFloatCookie)
}
datasourceCount, err := reader.ReadUnsignedLong()
if err != nil {
return errors.Wrap(err, 0)
}
rraCount, err := reader.ReadUnsignedLong()
if err != nil {
return errors.Wrap(err, 0)
}
pdpStep, err := reader.ReadUnsignedLong()
if err != nil {
return errors.Wrap(err, 0)
}
if _, err = reader.ReadUnivals(10); err != nil {
return errors.Wrap(err, 0)
}
f.header = &rrdRawHeader{
version: uint16(version),
datasourceCount: datasourceCount,
rraCount: rraCount,
pdpStep: pdpStep,
}
return nil
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:48,代码来源:rrd_raw_header.go
示例16: encodeRraCpdPreps
func (f *RrdRawFile) encodeRraCpdPreps(rraIndex, dsIndex int, rv reflect.Value) error {
if rv.Kind() != reflect.Struct {
return errors.Errorf("Cdp params must be a struct")
}
for i := 0; i < rv.Type().NumField(); i++ {
field := rv.Type().Field(i)
if field.Type.Kind() == reflect.Struct {
if err := f.encodeRraCpdPreps(rraIndex, dsIndex, rv.Field(i)); err != nil {
return err
}
continue
}
scratch := f.cdpPreps[rraIndex][dsIndex].scratch
tag := field.Tag.Get("cdp")
if tag == "" {
continue
}
if tag == "raw" && field.Type.Kind() == reflect.Slice {
switch field.Type.Elem().Kind() {
case reflect.Uint8:
raw := rv.Field(i).Interface().([]uint8)
values := f.dataFile.BytesToUnivals(raw)
for i, v := range values {
scratch[i] = v
}
default:
return errors.Errorf("cpd scatch must have type []uint64")
}
} else {
scratchIndex, err := strconv.ParseInt(tag, 10, 64)
if err != nil {
return err
}
switch field.Type.Kind() {
case reflect.Uint64:
scratch[scratchIndex] = univalForUnsignedLong(rv.Field(i).Uint())
case reflect.Float64:
scratch[scratchIndex] = univalForDouble(rv.Field(i).Float())
default:
return errors.Errorf("cpd field must have type uint64 or float64")
}
}
}
return nil
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:48,代码来源:rrd_raw_cdp_prep.go
示例17: PoliciesFromContext
func PoliciesFromContext(ctx context.Context) ([]policy.Policy, error) {
args, ok := ctx.Value(authKey).(*authorization)
if !ok {
return nil, errors.Errorf("Could not assert array type for %s", ctx.Value(authKey))
}
symbols := make([]policy.Policy, len(args.policies))
for i, arg := range args.policies {
symbols[i], ok = arg.(*policy.DefaultPolicy)
if !ok {
return nil, errors.Errorf("Could not assert policy type for %s", ctx.Value(authKey))
}
}
return symbols, nil
}
开发者ID:thanzen,项目名称:hydra,代码行数:16,代码来源:auth.go
示例18: SubjectFromContext
func SubjectFromContext(ctx context.Context) (string, error) {
args, ok := ctx.Value(authKey).(*authorization)
if !ok {
return "", errors.Errorf("Could not assert type for %v", ctx.Value(authKey))
}
return args.claims.GetSubject(), nil
}
开发者ID:thanzen,项目名称:hydra,代码行数:7,代码来源:auth.go
示例19: TokenFromContext
func TokenFromContext(ctx context.Context) (*jwt.Token, error) {
args, ok := ctx.Value(authKey).(*authorization)
if !ok {
return nil, errors.Errorf("Could not assert type for %v", ctx.Value(authKey))
}
return args.token, nil
}
开发者ID:thanzen,项目名称:hydra,代码行数:7,代码来源:auth.go
示例20: CalculatePdpPrep
func (d *DatasourceDerive) CalculatePdpPrep(newValue string, interval float64) (float64, error) {
if float64(d.Heartbeat) < interval {
d.LastValue = Undefined
}
rate := math.NaN()
newPdp := math.NaN()
if newValue != Undefined && float64(d.Heartbeat) >= interval {
newInt := new(big.Int)
_, err := fmt.Sscan(newValue, newInt)
if err != nil {
return math.NaN(), errors.Errorf("not a simple signed integer: %s", newValue)
}
if d.LastValue != "U" {
prevInt := new(big.Int)
_, err := fmt.Sscan(d.LastValue, prevInt)
if err != nil {
return math.NaN(), errors.Wrap(err, 0)
}
diff := new(big.Int)
diff.Sub(newInt, prevInt)
newPdp = float64(diff.Uint64())
rate = newPdp / interval
}
}
if !d.checkRateBounds(rate) {
newPdp = math.NaN()
}
d.LastValue = newValue
return newPdp, nil
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:35,代码来源:datasource_derive.go
注:本文中的github.com/go-errors/errors.Errorf函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论