Skip to content

Instantly share code, notes, and snippets.

@amwill04
Created February 10, 2020 22:31
Show Gist options
  • Save amwill04/a6272afebbddeb6e09f1bc3235019d4c to your computer and use it in GitHub Desktop.
Save amwill04/a6272afebbddeb6e09f1bc3235019d4c to your computer and use it in GitHub Desktop.
AWS Lambda Golang Error Capture
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"runtime/debug"
"time"
)
func main() {
lambda.Start(handler)
}
func handler() (rerr error) {
defer func () {
if err := recover(); err != nil {
rerr = fmt.Errorf(`{"message": "%s", "level": "error", "timestamp": "%s", "stackTrace": "%s"}`, err, time.Now().Format(time.RFC3339), debug.Stack())
}
}()
errChannel := make(chan error)
go forceError(errChannel)
err :=<- errChannel
return err
}
func forceError(errChannel chan<- error){
defer func () {
if err := recover(); err != nil {
errChannel <- fmt.Errorf(`{"message": "%s", "level": "error", "timestamp": "%s", "stackTrace": "%s"}`, err, time.Now().Format(time.RFC3339), debug.Stack())
}
}()
x := []int{1,2}
for i := 0;i < 3;i++{
fmt.Printf(`{"message": "number: %d", "level": "info", "timestamp": "%s"}\n`, x[i], time.Now().Format(time.RFC3339))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment