fix: recover panics and return JSON errors on CSV export failure
Prevent handler panics from dropping connections as nginx 502s and surface export DB errors as JSON 500 responses with CORS headers. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
aea9ebd60b
commit
2fa3459493
|
|
@ -50,6 +50,7 @@ import (
|
||||||
"github.com/bytedance/sonic"
|
"github.com/bytedance/sonic"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
|
|
@ -158,6 +159,7 @@ func NewApp(
|
||||||
ExposeHeaders: "Content-Disposition",
|
ExposeHeaders: "Content-Disposition",
|
||||||
// AllowCredentials: true,
|
// AllowCredentials: true,
|
||||||
}))
|
}))
|
||||||
|
app.Use(recover.New())
|
||||||
|
|
||||||
app.Static("/static", "./static")
|
app.Static("/static", "./static")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,9 @@ func exportRequestContext(c *fiber.Ctx) context.Context {
|
||||||
|
|
||||||
func (h *Handler) csvStreamErrorLogger(resource string) func(error) {
|
func (h *Handler) csvStreamErrorLogger(resource string) func(error) {
|
||||||
return func(err error) {
|
return func(err error) {
|
||||||
h.logger.Error("csv export stream failed", "resource", resource, "error", err)
|
h.logger.Error("csv export failed", "resource", resource, "error", err)
|
||||||
if h.mongoLoggerSvc != nil {
|
if h.mongoLoggerSvc != nil {
|
||||||
h.mongoLoggerSvc.Error("csv export stream failed",
|
h.mongoLoggerSvc.Error("csv export failed",
|
||||||
zap.String("resource", resource),
|
zap.String("resource", resource),
|
||||||
zap.Error(err),
|
zap.Error(err),
|
||||||
)
|
)
|
||||||
|
|
@ -37,6 +37,22 @@ func (h *Handler) csvStreamErrorLogger(resource string) func(error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) writeCSVExport(
|
||||||
|
c *fiber.Ctx,
|
||||||
|
resource string,
|
||||||
|
filename string,
|
||||||
|
headers []string,
|
||||||
|
fetchBatch func(offset, limit int32) ([][]string, error),
|
||||||
|
) error {
|
||||||
|
if err := response.StreamCSV(c, filename, headers, fetchBatch, h.csvStreamErrorLogger(resource)); err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||||
|
Message: fmt.Sprintf("Failed to export %s", resource),
|
||||||
|
Error: err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func csvOptionalString(p *string) string {
|
func csvOptionalString(p *string) string {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return ""
|
return ""
|
||||||
|
|
@ -108,7 +124,7 @@ func (h *Handler) ExportAdminPaymentsCSV(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := exportRequestContext(c)
|
ctx := exportRequestContext(c)
|
||||||
return response.StreamCSV(c, exportFilename("payments"), headers, func(offset, limit int32) ([][]string, error) {
|
return h.writeCSVExport(c, "payments", exportFilename("payments"), headers, func(offset, limit int32) ([][]string, error) {
|
||||||
filter.Limit = limit
|
filter.Limit = limit
|
||||||
filter.Offset = offset
|
filter.Offset = offset
|
||||||
filter.SkipTotalCount = true
|
filter.SkipTotalCount = true
|
||||||
|
|
@ -143,7 +159,7 @@ func (h *Handler) ExportAdminPaymentsCSV(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}, h.csvStreamErrorLogger("payments"))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportUsersCSV godoc
|
// ExportUsersCSV godoc
|
||||||
|
|
@ -172,7 +188,7 @@ func (h *Handler) ExportUsersCSV(c *fiber.Ctx) error {
|
||||||
|
|
||||||
ctx := exportRequestContext(c)
|
ctx := exportRequestContext(c)
|
||||||
batchSize := int32(response.ExportBatchSize)
|
batchSize := int32(response.ExportBatchSize)
|
||||||
return response.StreamCSV(c, exportFilename("users"), headers, func(offset, limit int32) ([][]string, error) {
|
return h.writeCSVExport(c, "users", exportFilename("users"), headers, func(offset, limit int32) ([][]string, error) {
|
||||||
filter.Page = int64(offset / batchSize)
|
filter.Page = int64(offset / batchSize)
|
||||||
filter.PageSize = int64(batchSize)
|
filter.PageSize = int64(batchSize)
|
||||||
users, _, err := h.userSvc.GetAllUsers(ctx, filter)
|
users, _, err := h.userSvc.GetAllUsers(ctx, filter)
|
||||||
|
|
@ -221,7 +237,7 @@ func (h *Handler) ExportUsersCSV(c *fiber.Ctx) error {
|
||||||
rows = rows[:limit]
|
rows = rows[:limit]
|
||||||
}
|
}
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}, h.csvStreamErrorLogger("users"))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportActivityLogsCSV godoc
|
// ExportActivityLogsCSV godoc
|
||||||
|
|
@ -245,7 +261,7 @@ func (h *Handler) ExportActivityLogsCSV(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := exportRequestContext(c)
|
ctx := exportRequestContext(c)
|
||||||
return response.StreamCSV(c, exportFilename("activity-logs"), headers, func(offset, limit int32) ([][]string, error) {
|
return h.writeCSVExport(c, "activity-logs", exportFilename("activity-logs"), headers, func(offset, limit int32) ([][]string, error) {
|
||||||
filter.Limit = limit
|
filter.Limit = limit
|
||||||
filter.Offset = offset
|
filter.Offset = offset
|
||||||
logs, _, err := h.activityLogSvc.List(ctx, filter)
|
logs, _, err := h.activityLogSvc.List(ctx, filter)
|
||||||
|
|
@ -273,7 +289,7 @@ func (h *Handler) ExportActivityLogsCSV(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}, h.csvStreamErrorLogger("activity-logs"))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportUserSubscriptionsCSV godoc
|
// ExportUserSubscriptionsCSV godoc
|
||||||
|
|
@ -299,7 +315,7 @@ func (h *Handler) ExportUserSubscriptionsCSV(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := exportRequestContext(c)
|
ctx := exportRequestContext(c)
|
||||||
return response.StreamCSV(c, exportFilename("subscriptions"), headers, func(offset, limit int32) ([][]string, error) {
|
return h.writeCSVExport(c, "subscriptions", exportFilename("subscriptions"), headers, func(offset, limit int32) ([][]string, error) {
|
||||||
filter.Limit = limit
|
filter.Limit = limit
|
||||||
filter.Offset = offset
|
filter.Offset = offset
|
||||||
page, err := h.subscriptionsSvc.ListUserSubscriptionsAdmin(ctx, filter)
|
page, err := h.subscriptionsSvc.ListUserSubscriptionsAdmin(ctx, filter)
|
||||||
|
|
@ -334,7 +350,7 @@ func (h *Handler) ExportUserSubscriptionsCSV(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}, h.csvStreamErrorLogger("subscriptions"))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExportTeamMembersCSV godoc
|
// ExportTeamMembersCSV godoc
|
||||||
|
|
@ -361,7 +377,7 @@ func (h *Handler) ExportTeamMembersCSV(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
rows := teamMembersToCSVRows(members)
|
rows := teamMembersToCSVRows(members)
|
||||||
return response.StreamCSV(c, exportFilename("team-members"), headers, func(offset, limit int32) ([][]string, error) {
|
return h.writeCSVExport(c, "team-members", exportFilename("team-members"), headers, func(offset, limit int32) ([][]string, error) {
|
||||||
if offset > 0 {
|
if offset > 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
@ -369,11 +385,11 @@ func (h *Handler) ExportTeamMembersCSV(c *fiber.Ctx) error {
|
||||||
return rows[:limit], nil
|
return rows[:limit], nil
|
||||||
}
|
}
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}, h.csvStreamErrorLogger("team-members"))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
batchSize := int32(response.ExportBatchSize)
|
batchSize := int32(response.ExportBatchSize)
|
||||||
return response.StreamCSV(c, exportFilename("team-members"), headers, func(offset, limit int32) ([][]string, error) {
|
return h.writeCSVExport(c, "team-members", exportFilename("team-members"), headers, func(offset, limit int32) ([][]string, error) {
|
||||||
filter.Page = int64(offset/batchSize) + 1
|
filter.Page = int64(offset/batchSize) + 1
|
||||||
filter.PageSize = int64(batchSize)
|
filter.PageSize = int64(batchSize)
|
||||||
members, _, err := h.teamSvc.GetAllTeamMembers(ctx, filter)
|
members, _, err := h.teamSvc.GetAllTeamMembers(ctx, filter)
|
||||||
|
|
@ -385,7 +401,7 @@ func (h *Handler) ExportTeamMembersCSV(c *fiber.Ctx) error {
|
||||||
rows = rows[:limit]
|
rows = rows[:limit]
|
||||||
}
|
}
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}, h.csvStreamErrorLogger("team-members"))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func int32CSV(p *int32) string {
|
func int32CSV(p *int32) string {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user