Created
February 10, 2020 22:31
-
-
Save amwill04/a6272afebbddeb6e09f1bc3235019d4c to your computer and use it in GitHub Desktop.
AWS Lambda Golang Error Capture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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