|
69 | 69 |
|
70 | 70 | return c.JSON(http.StatusOK, "concrete") |
71 | 71 | } |
| 72 | + |
| 73 | + panicHandler = func(c echo.Context) error { |
| 74 | + panic("test panic") |
| 75 | + } |
72 | 76 | ) |
73 | 77 |
|
74 | 78 | //nolint:maintidx |
@@ -700,6 +704,78 @@ func TestModuleWithEchoResources(t *testing.T) { |
700 | 704 | assert.Equal(t, "SAMEORIGIN", rec.Header().Get(echo.HeaderXFrameOptions)) // Secure middleware |
701 | 705 | } |
702 | 706 |
|
| 707 | +func TestModuleWithPanicRecoveryAndDebug(t *testing.T) { |
| 708 | + t.Setenv("APP_CONFIG_PATH", "testdata/config") |
| 709 | + t.Setenv("APP_DEBUG", "true") |
| 710 | + |
| 711 | + var httpServer *echo.Echo |
| 712 | + var logBuffer logtest.TestLogBuffer |
| 713 | + var traceExporter tracetest.TestTraceExporter |
| 714 | + var metricsRegistry *prometheus.Registry |
| 715 | + |
| 716 | + fxtest.New( |
| 717 | + t, |
| 718 | + fx.NopLogger, |
| 719 | + fxconfig.FxConfigModule, |
| 720 | + fxlog.FxLogModule, |
| 721 | + fxtrace.FxTraceModule, |
| 722 | + fxmetrics.FxMetricsModule, |
| 723 | + fxgenerate.FxGenerateModule, |
| 724 | + fxhttpserver.FxHttpServerModule, |
| 725 | + fx.Provide(service.NewTestService), |
| 726 | + fx.Options( |
| 727 | + fxhttpserver.AsHandler("GET", "/panic", panicHandler), |
| 728 | + ), |
| 729 | + fx.Populate(&httpServer, &logBuffer, &traceExporter, &metricsRegistry), |
| 730 | + ).RequireStart().RequireStop() |
| 731 | + |
| 732 | + // [GET] /bar |
| 733 | + req := httptest.NewRequest(http.MethodGet, "/panic", nil) |
| 734 | + rec := httptest.NewRecorder() |
| 735 | + httpServer.ServeHTTP(rec, req) |
| 736 | + |
| 737 | + assert.Equal(t, http.StatusInternalServerError, rec.Code) |
| 738 | + assert.Contains(t, rec.Body.String(), `"message": "test panic"`) |
| 739 | + assert.Contains(t, rec.Body.String(), `"stack": "*errors.errorString test panic`) |
| 740 | + |
| 741 | + logtest.AssertContainLogRecord(t, logBuffer, map[string]interface{}{ |
| 742 | + "level": "error", |
| 743 | + "service": "test", |
| 744 | + "module": "httpserver", |
| 745 | + "message": "[PANIC RECOVER] test panic", |
| 746 | + }) |
| 747 | + |
| 748 | + logtest.AssertContainLogRecord(t, logBuffer, map[string]interface{}{ |
| 749 | + "level": "error", |
| 750 | + "error": "test panic", |
| 751 | + "service": "test", |
| 752 | + "module": "httpserver", |
| 753 | + "stack": "*errors.errorString test panic", |
| 754 | + "message": "error handler", |
| 755 | + }) |
| 756 | + |
| 757 | + tracetest.AssertHasTraceSpan( |
| 758 | + t, |
| 759 | + traceExporter, |
| 760 | + "GET /panic", |
| 761 | + semconv.HTTPMethod(http.MethodGet), |
| 762 | + semconv.HTTPRoute("/panic"), |
| 763 | + semconv.HTTPStatusCode(http.StatusInternalServerError), |
| 764 | + ) |
| 765 | + |
| 766 | + expectedMetric := ` |
| 767 | + # HELP http_server_requests_total Number of processed HTTP requests |
| 768 | + # TYPE http_server_requests_total counter |
| 769 | + http_server_requests_total{method="GET",path="/panic",status="5xx"} 1 |
| 770 | + ` |
| 771 | + err := testutil.GatherAndCompare( |
| 772 | + metricsRegistry, |
| 773 | + strings.NewReader(expectedMetric), |
| 774 | + "http_server_requests_total", |
| 775 | + ) |
| 776 | + assert.NoError(t, err) |
| 777 | +} |
| 778 | + |
703 | 779 | func TestModuleWithMetrics(t *testing.T) { |
704 | 780 | t.Setenv("APP_CONFIG_PATH", "testdata/config") |
705 | 781 | t.Setenv("APP_DEBUG", "true") |
|
0 commit comments