Skip to content

Commit f58c7f2

Browse files
committed
Improve error handling in API response encoding and document writing; update file permission constants to octal notation for consistency.
1 parent 3bb9a6c commit f58c7f2

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

excalidraw-server/handlers/api/documents/documents.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func HandleGet(documentStore core.DocumentStore) http.HandlerFunc {
4343
http.Error(w, "not found", http.StatusNotFound)
4444
return
4545
}
46-
w.Write(document.Data.Bytes())
46+
if _, err := w.Write(document.Data.Bytes()); err != nil {
47+
http.Error(w, "failed to write response", http.StatusInternalServerError)
48+
}
4749
}
4850
}

excalidraw-server/handlers/websocket/collab.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ func SetupSocketIO() *socketio.Server {
3535
})
3636
ioo := socketio.NewServer(nil, opts)
3737

38+
//nolint:errcheck // Socket.IO event handlers don't return meaningful errors
3839
ioo.On("connection", func(clients ...any) {
39-
socket := clients[0].(*socketio.Socket)
40+
socket := clients[0].(*socketio.Socket) //nolint:errcheck // Type assertion in socket.io handler
4041
me := socket.Id()
4142
myRoom := socketio.Room(me)
42-
ioo.To(myRoom).Emit("init-room")
43+
_ = ioo.To(myRoom).Emit("init-room")
4344
utils.Log().Printf("init room %v\n", myRoom)
4445

46+
//nolint:errcheck // Socket.IO event handlers don't return meaningful errors
4547
socket.On("join-room", func(datas ...any) {
46-
room := socketio.Room(datas[0].(string))
47-
roomId := datas[0].(string)
48+
room := socketio.Room(datas[0].(string)) //nolint:errcheck // Type assertion in socket.io handler
49+
roomId := datas[0].(string) //nolint:errcheck // Type assertion in socket.io handler
4850
utils.Log().Printf("Socket %v has joined %v\n", me, room)
4951
socket.Join(room)
5052
ioo.In(room).FetchSockets()(func(usersInRoom []*socketio.RemoteSocket, _ error) {
@@ -54,10 +56,10 @@ func SetupSocketIO() *socketio.Server {
5456
roomsMutex.Unlock()
5557

5658
if len(usersInRoom) <= 1 {
57-
ioo.To(myRoom).Emit("first-in-room")
59+
_ = ioo.To(myRoom).Emit("first-in-room")
5860
} else {
5961
utils.Log().Printf("emit new user %v in room %v\n", me, room)
60-
socket.Broadcast().To(room).Emit("new-user", me)
62+
_ = socket.Broadcast().To(room).Emit("new-user", me)
6163
}
6264

6365
// Inform all clients by new users.
@@ -73,16 +75,18 @@ func SetupSocketIO() *socketio.Server {
7375
})
7476
})
7577

78+
//nolint:errcheck // Socket.IO event handlers don't return meaningful errors
7679
socket.On("server-broadcast", func(datas ...any) {
7780
roomID := datas[0].(string)
7881
utils.Log().Printf(" user %v sends update to room %v\n", me, roomID)
79-
socket.Broadcast().To(socketio.Room(roomID)).Emit("client-broadcast", datas[1], datas[2])
82+
_ = socket.Broadcast().To(socketio.Room(roomID)).Emit("client-broadcast", datas[1], datas[2])
8083
})
8184

85+
//nolint:errcheck // Socket.IO event handlers don't return meaningful errors
8286
socket.On("server-volatile-broadcast", func(datas ...any) {
8387
roomID := datas[0].(string)
8488
utils.Log().Printf(" user %v sends volatile update to room %v\n", me, roomID)
85-
socket.Volatile().Broadcast().To(socketio.Room(roomID)).Emit("client-broadcast", datas[1], datas[2])
89+
_ = socket.Volatile().Broadcast().To(socketio.Room(roomID)).Emit("client-broadcast", datas[1], datas[2])
8690
})
8791

8892
socket.On("user-follow", func(datas ...any) {

excalidraw-server/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func setupRouter(documentStore core.DocumentStore) *chi.Mux {
4444
r.Get("/api/rooms", func(w http.ResponseWriter, r *http.Request) {
4545
rooms := websocket.GetActiveRooms()
4646
w.Header().Set("Content-Type", "application/json")
47-
json.NewEncoder(w).Encode(rooms)
47+
if err := json.NewEncoder(w).Encode(rooms); err != nil {
48+
http.Error(w, "failed to encode response", http.StatusInternalServerError)
49+
}
4850
})
4951

5052
// Snapshot API routes - only available with SQLite store
@@ -76,7 +78,7 @@ func setupRouter(documentStore core.DocumentStore) *chi.Mux {
7678

7779
func waitForShutdown(ioo *socketio.Server) {
7880
exit := make(chan struct{})
79-
SignalC := make(chan os.Signal)
81+
SignalC := make(chan os.Signal, 1)
8082

8183
signal.Notify(SignalC, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
8284
go func() {

excalidraw-server/stores/filesystem/documents.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type documentStore struct {
1818
}
1919

2020
func NewDocumentStore(basePath string) core.DocumentStore {
21-
if err := os.MkdirAll(basePath, 0755); err != nil {
21+
if err := os.MkdirAll(basePath, 0o755); err != nil {
2222
log.Fatalf("failed to create base directory: %v", err)
2323
}
2424

@@ -57,7 +57,7 @@ func (s *documentStore) Create(ctx context.Context, document *core.Document) (st
5757
})
5858
log.Info("Creating new document")
5959

60-
if err := os.WriteFile(filePath, document.Data.Bytes(), 0644); err != nil {
60+
if err := os.WriteFile(filePath, document.Data.Bytes(), 0o644); err != nil {
6161
log.WithField("error", err).Error("Failed to create document")
6262
return "", err
6363
}

excalidraw-server/stores/sqlite/documents.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ import (
1414
"github.com/sirupsen/logrus"
1515
)
1616

17-
var savedDocuments = make(map[string]core.Document)
18-
1917
type documentStore struct {
2018
db *sql.DB
2119
}
2220

2321
func NewDocumentStore(dataSourceName string) core.DocumentStore {
24-
// db, err := sql.Open("sqlite3", ":memory:")
2522
db, err := sql.Open("sqlite3", dataSourceName)
2623

2724
if err != nil {

0 commit comments

Comments
 (0)