github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libfs/error_file.go (about)

     1  // Copyright 2015-2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libfs
     6  
     7  import (
     8  	"time"
     9  
    10  	goerrors "github.com/go-errors/errors"
    11  	"github.com/keybase/client/go/kbfs/libkbfs"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  // JSONReportedError stringifies the reported error before marshalling
    16  type JSONReportedError struct {
    17  	Time  time.Time
    18  	Error string
    19  	Stack []goerrors.StackFrame
    20  }
    21  
    22  func convertStack(stack []uintptr) []goerrors.StackFrame {
    23  	frames := make([]goerrors.StackFrame, len(stack))
    24  	for i, pc := range stack {
    25  		// TODO: Handle panics correctly, as described in the
    26  		// docs for runtime.Callers().
    27  		frames[i] = goerrors.NewStackFrame(pc)
    28  	}
    29  	return frames
    30  }
    31  
    32  // GetEncodedErrors gets the list of encoded errors in a format suitable
    33  // for error file.
    34  func GetEncodedErrors(config libkbfs.Config) func(context.Context) ([]byte, time.Time, error) {
    35  	return func(_ context.Context) ([]byte, time.Time, error) {
    36  		errors := config.Reporter().AllKnownErrors()
    37  		jsonErrors := make([]JSONReportedError, len(errors))
    38  		for i, e := range errors {
    39  			jsonErrors[i].Time = e.Time
    40  			jsonErrors[i].Error = e.Error.Error()
    41  			jsonErrors[i].Stack = convertStack(e.Stack)
    42  		}
    43  		data, err := PrettyJSON(jsonErrors)
    44  		var t time.Time
    45  		if len(errors) > 0 {
    46  			t = errors[len(errors)-1].Time
    47  		}
    48  		return data, t, err
    49  	}
    50  }