11package cube
22
33import (
4+ "fmt"
5+ "net/http"
6+ "runtime"
47 "time"
58
69 "github.com/labstack/echo"
@@ -14,12 +17,6 @@ type (
1417 // Skipper defines a function to skip middleware.
1518 Skipper middleware.Skipper
1619
17- // App ID
18- AppID string
19-
20- // App name
21- AppName string
22-
2320 // LabStack Account ID
2421 AccountID string `json:"account_id"`
2522
3229 // Interval in seconds to dispatch the batch
3330 DispatchInterval time.Duration `json:"dispatch_interval"`
3431
32+ // Additional tags
33+ Tags []string `json:"tags"`
34+
3535 // TODO: To be implemented
3636 ClientLookup string `json:"client_lookup"`
3737 }
@@ -74,8 +74,6 @@ func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
7474 // Initialize
7575 client := labstack .NewClient (config .AccountID , config .APIKey )
7676 cube := client .Cube ()
77- cube .AppID = config .AppID
78- cube .AppName = config .AppName
7977 cube .APIKey = config .APIKey
8078 cube .BatchSize = config .BatchSize
8179 cube .DispatchInterval = config .DispatchInterval
@@ -86,12 +84,39 @@ func MiddlewareWithConfig(config Config) echo.MiddlewareFunc {
8684 if config .Skipper (c ) {
8785 return next (c )
8886 }
89- r := cube .Start (c .Request (), c .Response ())
87+
88+ // Start
89+ cr := cube .Start (c .Request (), c .Response ())
90+
91+ // Handle panic
92+ defer func () {
93+ if r := recover (); r != nil {
94+ switch r := r .(type ) {
95+ case error :
96+ err = r
97+ default :
98+ err = fmt .Errorf ("%v" , r )
99+ }
100+ stack := make ([]byte , 4 << 10 ) // 4 KB
101+ length := runtime .Stack (stack , false )
102+ cr .Error = err .Error ()
103+ cr .StackTrace = string (stack [:length ])
104+ println (c .Response ().Status )
105+ if c .Response ().Status == http .StatusOK {
106+ c .Response ().Status = http .StatusInternalServerError
107+ }
108+ }
109+
110+ // Stop
111+ cube .Stop (cr , c .Response ().Status , c .Response ().Size )
112+ }()
113+
114+ // Next
90115 if err = next (c ); err != nil {
91116 c .Error (err )
92117 }
93- cube . Stop ( r , c . Response (). Status , c . Response (). Size )
94- return
118+
119+ return nil
95120 }
96121 }
97122}
0 commit comments