Add comprehensive error handling and logging to scenario_map endpoint

- Add try-catch blocks to prevent 500 errors
- Add logging to help diagnose issues
- Add nil checks for scenario_data and rooms
- Skip individual rooms that fail to process
- Return descriptive error messages for debugging
This commit is contained in:
Z. Cliffe Schreuders
2025-11-22 00:46:55 +00:00
parent a945859730
commit 25dff05d39

View File

@@ -21,24 +21,47 @@ module BreakEscape
def scenario_map
authorize @game if defined?(Pundit)
# Return minimal room/connection metadata without contents
layout = {}
@game.scenario_data['rooms'].each do |room_id, room_data|
layout[room_id] = {
type: room_data['type'],
connections: room_data['connections'] || {},
locked: room_data['locked'] || false,
lockType: room_data['lockType'],
hasNPCs: (room_data['npcs']&.length || 0) > 0,
accessible: @game.room_unlocked?(room_id)
}
end
begin
# Check if scenario_data exists
unless @game.scenario_data.present?
Rails.logger.error "[BreakEscape] scenario_map: Game #{@game.id} has no scenario_data"
return render_error('Scenario data not available', :internal_server_error)
end
render json: {
startRoom: @game.scenario_data['startRoom'],
currentRoom: @game.player_state['currentRoom'],
rooms: layout
}
# Return minimal room/connection metadata without contents
layout = {}
rooms = @game.scenario_data['rooms'] || {}
Rails.logger.debug "[BreakEscape] scenario_map: Processing #{rooms.keys.length rescue 0} rooms"
rooms.each do |room_id, room_data|
next unless room_data.is_a?(Hash)
begin
layout[room_id] = {
type: room_data['type'],
connections: room_data['connections'] || {},
locked: room_data['locked'] || false,
lockType: room_data['lockType'],
hasNPCs: (room_data['npcs']&.length || 0) > 0,
accessible: @game.room_unlocked?(room_id)
}
rescue => e
Rails.logger.error "[BreakEscape] Error processing room #{room_id}: #{e.message}"
# Skip this room and continue
next
end
end
render json: {
startRoom: @game.scenario_data['startRoom'],
currentRoom: @game.player_state['currentRoom'],
rooms: layout
}
rescue => e
Rails.logger.error "[BreakEscape] scenario_map error: #{e.message}\n#{e.backtrace.first(5).join("\n")}"
render_error("Failed to generate scenario map: #{e.message}", :internal_server_error)
end
end
# GET /games/:id/room/:room_id