[PR #5194] [MERGED] Improve config loading error messages with line numbers #5162

Closed
opened 2026-05-05 14:55:52 -06:00 by gitea-mirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/fatedier/frp/pull/5194
Author: @fatedier
Created: 3/4/2026
Status: Merged
Merged: 3/4/2026
Merged by: @fatedier

Base: devHead: claude/frpc-verify-error-details-EW9Pn


📝 Commits (1)

  • 8d000d0 pkg/config: improve error messages with line number details for config parsing

📊 Changes

2 files changed (+200 additions, -6 deletions)

View changed files

📝 pkg/config/load.go (+94 -6)
📝 pkg/config/load_test.go (+106 -0)

📄 Description

WHY

Configuration file parsing errors can be difficult to debug without knowing where in the file the problem occurs. This change enhances error reporting for TOML, JSON, and YAML configuration files by:

  1. Adding line number information to TOML syntax errors
  2. Adding field path and line number information to type mismatch errors
  3. Automatically detecting file format from file extension to enable better error context
  4. Making error messages more actionable for users troubleshooting configuration issues

WHAT

Core Changes:

  • Modified LoadConfigure() to accept an optional format hint parameter
  • Updated LoadConfigureFromFile() to detect format from file extension and pass it to LoadConfigure()
  • Added detectFormatFromPath() to determine config format from file extension (.toml, .yaml, .yml, .json)
  • Added formatTOMLError() to extract and format TOML decode errors with line/column information
  • Added enhanceDecodeError() to enhance JSON/YAML errors with field path and line number information
  • Added findFieldLineInContent() to locate field definitions in the original config content

Error Message Improvements:

  • TOML syntax errors now include: toml: line X, column Y: error message
  • Type mismatch errors now include: line X: field "fieldname": cannot unmarshal value into type
  • Field information is preserved in error messages for better debugging

Tests Added:

  • TestTOMLSyntaxErrorWithLineNumber: Verifies TOML syntax errors include line information
  • TestTOMLTypeMismatchErrorWithFieldInfo: Verifies type errors include field information
  • TestFindFieldLineInContent: Tests field line detection across various config structures
  • TestFormatDetection: Tests file format detection from extensions
  • TestValidTOMLStillWorks: Ensures valid configs continue to parse correctly

Test Plan

All new functionality is covered by unit tests in pkg/config/load_test.go. The changes are backward compatible—existing code paths continue to work as before, with enhanced error messages as a bonus.

https://claude.ai/code/session_017HWLfcXS3U2hLoy4dsg8Nv


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/fatedier/frp/pull/5194 **Author:** [@fatedier](https://github.com/fatedier) **Created:** 3/4/2026 **Status:** ✅ Merged **Merged:** 3/4/2026 **Merged by:** [@fatedier](https://github.com/fatedier) **Base:** `dev` ← **Head:** `claude/frpc-verify-error-details-EW9Pn` --- ### 📝 Commits (1) - [`8d000d0`](https://github.com/fatedier/frp/commit/8d000d0e82940aab54c4bc7fbf88f6edb77a6cb2) pkg/config: improve error messages with line number details for config parsing ### 📊 Changes **2 files changed** (+200 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `pkg/config/load.go` (+94 -6) 📝 `pkg/config/load_test.go` (+106 -0) </details> ### 📄 Description ### WHY Configuration file parsing errors can be difficult to debug without knowing where in the file the problem occurs. This change enhances error reporting for TOML, JSON, and YAML configuration files by: 1. Adding line number information to TOML syntax errors 2. Adding field path and line number information to type mismatch errors 3. Automatically detecting file format from file extension to enable better error context 4. Making error messages more actionable for users troubleshooting configuration issues ### WHAT **Core Changes:** - Modified `LoadConfigure()` to accept an optional format hint parameter - Updated `LoadConfigureFromFile()` to detect format from file extension and pass it to `LoadConfigure()` - Added `detectFormatFromPath()` to determine config format from file extension (.toml, .yaml, .yml, .json) - Added `formatTOMLError()` to extract and format TOML decode errors with line/column information - Added `enhanceDecodeError()` to enhance JSON/YAML errors with field path and line number information - Added `findFieldLineInContent()` to locate field definitions in the original config content **Error Message Improvements:** - TOML syntax errors now include: `toml: line X, column Y: error message` - Type mismatch errors now include: `line X: field "fieldname": cannot unmarshal value into type` - Field information is preserved in error messages for better debugging **Tests Added:** - `TestTOMLSyntaxErrorWithLineNumber`: Verifies TOML syntax errors include line information - `TestTOMLTypeMismatchErrorWithFieldInfo`: Verifies type errors include field information - `TestFindFieldLineInContent`: Tests field line detection across various config structures - `TestFormatDetection`: Tests file format detection from extensions - `TestValidTOMLStillWorks`: Ensures valid configs continue to parse correctly ### Test Plan All new functionality is covered by unit tests in `pkg/config/load_test.go`. The changes are backward compatible—existing code paths continue to work as before, with enhanced error messages as a bonus. https://claude.ai/code/session_017HWLfcXS3U2hLoy4dsg8Nv --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
gitea-mirror 2026-05-05 14:55:52 -06:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/frp#5162
No description provided.