Skip to content

Commit 6d71e62

Browse files
committed
chore: more verbose logging
1 parent f99d309 commit 6d71e62

6 files changed

Lines changed: 235 additions & 44 deletions

File tree

docs/docs.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ const docTemplate = `{
166166
"druid",
167167
"daemon"
168168
],
169-
"summary": "Enable development mode",
170169
"operationId": "enableDev",
171170
"responses": {
172171
"200": {
@@ -559,7 +558,7 @@ const docTemplate = `{
559558
"*/*"
560559
],
561560
"produces": [
562-
"text/html"
561+
"application/json"
563562
],
564563
"tags": [
565564
"ui",
@@ -570,7 +569,31 @@ const docTemplate = `{
570569
"operationId": "getPrivateUIIndex",
571570
"responses": {
572571
"200": {
573-
"description": "OK"
572+
"description": "List of files in private UI directory",
573+
"schema": {
574+
"type": "array",
575+
"items": {
576+
"type": "string"
577+
}
578+
}
579+
},
580+
"404": {
581+
"description": "Private UI directory not found",
582+
"schema": {
583+
"type": "object",
584+
"additionalProperties": {
585+
"type": "string"
586+
}
587+
}
588+
},
589+
"500": {
590+
"description": "Internal server error with details",
591+
"schema": {
592+
"type": "object",
593+
"additionalProperties": {
594+
"type": "string"
595+
}
596+
}
574597
}
575598
}
576599
}
@@ -581,7 +604,7 @@ const docTemplate = `{
581604
"*/*"
582605
],
583606
"produces": [
584-
"text/html"
607+
"application/json"
585608
],
586609
"tags": [
587610
"ui",
@@ -592,7 +615,31 @@ const docTemplate = `{
592615
"operationId": "getPublicUIIndex",
593616
"responses": {
594617
"200": {
595-
"description": "OK"
618+
"description": "List of files in public UI directory",
619+
"schema": {
620+
"type": "array",
621+
"items": {
622+
"type": "string"
623+
}
624+
}
625+
},
626+
"404": {
627+
"description": "Public UI directory not found",
628+
"schema": {
629+
"type": "object",
630+
"additionalProperties": {
631+
"type": "string"
632+
}
633+
}
634+
},
635+
"500": {
636+
"description": "Internal server error with details",
637+
"schema": {
638+
"type": "object",
639+
"additionalProperties": {
640+
"type": "string"
641+
}
642+
}
596643
}
597644
}
598645
}

docs/swagger.json

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@
158158
"druid",
159159
"daemon"
160160
],
161-
"summary": "Enable development mode",
162161
"operationId": "enableDev",
163162
"responses": {
164163
"200": {
@@ -551,7 +550,7 @@
551550
"*/*"
552551
],
553552
"produces": [
554-
"text/html"
553+
"application/json"
555554
],
556555
"tags": [
557556
"ui",
@@ -562,7 +561,31 @@
562561
"operationId": "getPrivateUIIndex",
563562
"responses": {
564563
"200": {
565-
"description": "OK"
564+
"description": "List of files in private UI directory",
565+
"schema": {
566+
"type": "array",
567+
"items": {
568+
"type": "string"
569+
}
570+
}
571+
},
572+
"404": {
573+
"description": "Private UI directory not found",
574+
"schema": {
575+
"type": "object",
576+
"additionalProperties": {
577+
"type": "string"
578+
}
579+
}
580+
},
581+
"500": {
582+
"description": "Internal server error with details",
583+
"schema": {
584+
"type": "object",
585+
"additionalProperties": {
586+
"type": "string"
587+
}
588+
}
566589
}
567590
}
568591
}
@@ -573,7 +596,7 @@
573596
"*/*"
574597
],
575598
"produces": [
576-
"text/html"
599+
"application/json"
577600
],
578601
"tags": [
579602
"ui",
@@ -584,7 +607,31 @@
584607
"operationId": "getPublicUIIndex",
585608
"responses": {
586609
"200": {
587-
"description": "OK"
610+
"description": "List of files in public UI directory",
611+
"schema": {
612+
"type": "array",
613+
"items": {
614+
"type": "string"
615+
}
616+
}
617+
},
618+
"404": {
619+
"description": "Public UI directory not found",
620+
"schema": {
621+
"type": "object",
622+
"additionalProperties": {
623+
"type": "string"
624+
}
625+
}
626+
},
627+
"500": {
628+
"description": "Internal server error with details",
629+
"schema": {
630+
"type": "object",
631+
"additionalProperties": {
632+
"type": "string"
633+
}
634+
}
588635
}
589636
}
590637
}

docs/swagger.yaml

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ paths:
453453
description: Internal Server Error
454454
schema:
455455
$ref: '#/definitions/ErrorResponse'
456-
summary: Enable development mode
457456
tags:
458457
- ui
459458
- dev
@@ -716,10 +715,26 @@ paths:
716715
- '*/*'
717716
operationId: getPrivateUIIndex
718717
produces:
719-
- text/html
718+
- application/json
720719
responses:
721720
"200":
722-
description: OK
721+
description: List of files in private UI directory
722+
schema:
723+
items:
724+
type: string
725+
type: array
726+
"404":
727+
description: Private UI directory not found
728+
schema:
729+
additionalProperties:
730+
type: string
731+
type: object
732+
"500":
733+
description: Internal server error with details
734+
schema:
735+
additionalProperties:
736+
type: string
737+
type: object
723738
summary: Serve private UI index
724739
tags:
725740
- ui
@@ -731,10 +746,26 @@ paths:
731746
- '*/*'
732747
operationId: getPublicUIIndex
733748
produces:
734-
- text/html
749+
- application/json
735750
responses:
736751
"200":
737-
description: OK
752+
description: List of files in public UI directory
753+
schema:
754+
items:
755+
type: string
756+
type: array
757+
"404":
758+
description: Public UI directory not found
759+
schema:
760+
additionalProperties:
761+
type: string
762+
type: object
763+
"500":
764+
description: Internal server error with details
765+
schema:
766+
additionalProperties:
767+
type: string
768+
type: object
738769
summary: Serve public UI index
739770
tags:
740771
- ui

internal/core/services/ui_service.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package services
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67

@@ -23,26 +24,39 @@ func NewUiService(scrollService ports.ScrollServiceInterface) ports.UiServiceInt
2324
// This method validates the file exists and is accessible before serving
2425
func (us *UiService) GetIndex(filePath string) ([]string, error) {
2526
scrollDir := us.scrollService.GetDir()
26-
2727
uiDir := scrollDir + "/" + filePath
2828

29+
// Check if directory exists
30+
info, err := os.Stat(uiDir)
31+
if err != nil {
32+
if os.IsNotExist(err) {
33+
return nil, os.ErrNotExist
34+
}
35+
return nil, err // This will be handled in the handler for a 500 error, but let's make it more informative
36+
}
37+
if !info.IsDir() {
38+
return nil, os.ErrInvalid
39+
}
40+
2941
fileList := []string{}
30-
err := filepath.Walk(uiDir, func(path string, info os.FileInfo, err error) error {
42+
walkErr := filepath.Walk(uiDir, func(path string, info os.FileInfo, err error) error {
3143
if err != nil {
3244
return err
3345
}
3446
if !info.IsDir() {
35-
3647
if relPath, err := filepath.Rel(uiDir, path); err == nil {
3748
fileList = append(fileList, relPath)
3849
} else {
39-
// Fallback to original path if relative path calculation fails
4050
fileList = append(fileList, path)
4151
}
4252
}
4353
return nil
4454
})
45-
return fileList, err
55+
if walkErr != nil {
56+
// Return a wrapped error for more informative 500 error
57+
return nil, fmt.Errorf("failed to walk directory '%s': %w", uiDir, walkErr)
58+
}
59+
return fileList, nil
4660
}
4761

4862
// Ensure UiService implements UiServiceInterface at compile time

internal/handler/ui_handler.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package handler
22

33
import (
4+
"errors"
5+
"os"
6+
47
"github.com/gofiber/fiber/v2"
58
"github.com/highcard-dev/daemon/internal/core/ports"
69
)
@@ -19,13 +22,20 @@ func NewUiHandler(uiService ports.UiServiceInterface) *UiHandler {
1922
// @ID getPublicUIIndex
2023
// @Tags ui, druid, daemon
2124
// @Accept */*
22-
// @Produce html
23-
// @Success 200
25+
// @Produce json
26+
// @Success 200 {array} string "List of files in public UI directory"
27+
// @Failure 404 {object} map[string]string "Public UI directory not found"
28+
// @Failure 500 {object} map[string]string "Internal server error with details"
2429
// @Router /public/index [get]
2530
func (uh *UiHandler) PublicIndex(ctx *fiber.Ctx) error {
2631
files, err := uh.uiService.GetIndex("public")
2732
if err != nil {
28-
return ctx.Status(500).SendString("Failed to retrieve public UI index")
33+
if errors.Is(err, os.ErrNotExist) {
34+
return ctx.Status(fiber.StatusNotFound).SendString("Public UI directory not found")
35+
}
36+
return ctx.Status(500).JSON(fiber.Map{
37+
"error": err.Error(),
38+
})
2939
}
3040
return ctx.JSON(files)
3141
}
@@ -34,13 +44,20 @@ func (uh *UiHandler) PublicIndex(ctx *fiber.Ctx) error {
3444
// @ID getPrivateUIIndex
3545
// @Tags ui, druid, daemon
3646
// @Accept */*
37-
// @Produce html
38-
// @Success 200
47+
// @Produce json
48+
// @Success 200 {array} string "List of files in private UI directory"
49+
// @Failure 404 {object} map[string]string "Private UI directory not found"
50+
// @Failure 500 {object} map[string]string "Internal server error with details"
3951
// @Router /private/index [get]
4052
func (uh *UiHandler) PrivateIndex(ctx *fiber.Ctx) error {
4153
files, err := uh.uiService.GetIndex("private")
4254
if err != nil {
43-
return ctx.Status(500).SendString("Failed to retrieve private UI index")
55+
if errors.Is(err, os.ErrNotExist) {
56+
return ctx.Status(fiber.StatusNotFound).SendString("Private UI directory not found")
57+
}
58+
return ctx.Status(500).JSON(fiber.Map{
59+
"error": err.Error(),
60+
})
4461
}
4562
return ctx.JSON(files)
4663
}

0 commit comments

Comments
 (0)