...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/workflow/errors/jsonify_error.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/workflow/errors

     1  // Copyright 2020 Chaos Mesh Authors.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package errors
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  	"reflect"
    20  
    21  	"golang.org/x/xerrors"
    22  )
    23  
    24  func toJsonOrFallbackToError(origin error) string {
    25  	out, err := json.Marshal(origin)
    26  	if err != nil {
    27  		if wrapper, ok := err.(xerrors.Wrapper); ok {
    28  			return fmt.Sprintf(
    29  				"failed to jsonify error on type %s, json error: %s; origin error message: %s",
    30  				reflect.TypeOf(origin).Name(),
    31  				err,
    32  				wrapper.Unwrap().Error(),
    33  			)
    34  		}
    35  		return fmt.Sprintf(
    36  			"failed to jsonify error on type %s, json error, %s; also failed to Unwrap() on it.",
    37  			reflect.TypeOf(origin).Name(),
    38  			err,
    39  		)
    40  
    41  	}
    42  	return string(out)
    43  }
    44