49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package lib
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"runtime/debug"
|
|
"time"
|
|
)
|
|
|
|
const errorsDir = "/tank/inou/errors"
|
|
|
|
type Incident struct {
|
|
ID string `json:"id"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
Context string `json:"context"`
|
|
Error string `json:"error"`
|
|
Details map[string]interface{} `json:"details"`
|
|
Stack string `json:"stack"`
|
|
}
|
|
|
|
func randomHex(n int) string {
|
|
b := make([]byte, n)
|
|
rand.Read(b)
|
|
return hex.EncodeToString(b)
|
|
}
|
|
|
|
func SendErrorForAnalysis(context string, err error, details map[string]interface{}) {
|
|
id := fmt.Sprintf("%s-%s", time.Now().Format("20060102-150405"), randomHex(4))
|
|
|
|
incident := Incident{
|
|
ID: id,
|
|
Timestamp: time.Now(),
|
|
Context: context,
|
|
Error: err.Error(),
|
|
Details: details,
|
|
Stack: string(debug.Stack()),
|
|
}
|
|
|
|
os.MkdirAll(errorsDir, 0755)
|
|
path := fmt.Sprintf("%s/%s.json", errorsDir, id)
|
|
data, _ := json.MarshalIndent(incident, "", " ")
|
|
os.WriteFile(path, data, 0644)
|
|
|
|
SendSignal(fmt.Sprintf("[inou] %s: %v\n%s", context, err, path))
|
|
}
|