Skip to content

Conversation

@marxoffice
Copy link

Improved Error Reporting Mechanism - Pull Request

Overview

This patch includes two major improvements aimed at enhancing the error reporting friendliness.

Commit Contents

Commit 1: Improved Error Reporting for Invalid Default Values (5058a89)

Key Improvements:

  • Added detailed error wrapping mechanism in the setField function
  • Error messages now include field name, default value, and original error details
  • Modified error handling logic for all basic data types

Error Report Example:

type ErrIntDefault struct {
	ErrInt int `default:"abc"`
}

will report error

error on set Field:[ErrInt], DefaultValue:[abc] Error:[strconv.ParseInt: parsing "abc": invalid syntax]

Commit 2: Improved Subfield Error Reporting (df4441b)

Key Improvements:

  • Fixed the issue of error propagation in nested structures
  • Ensured that errors in pointer type fields are correctly propagated upward

Test Case Example:

type SubErrorDefault struct {
    ErrSlice  []string \`default:"[1,2,3]"\`  // Error here, correct default value is ["1", "2", "3"]
    NormalInt int      \`default:"1"\`
}

type ParentErrorDefault struct {
    NormalInt  int              \`default:"1"\`
    ErrChild   *SubErrorDefault \`default:"{}"\`
    NormalBool bool             \`default:"true"\`
}

// Before This Commit
Set(&SubErrorDefault{}) // will report error because parse json failed
Set(&ParentErrorDefault{}) // will not report error

// After This Commit
Set(&SubErrorDefault{}) // will report error because parse json failed
Set(&ParentErrorDefault{}) // will report error because set ErrChild failed

Usage Example

When using invalid default values in configuration structures, clear error information is now provided:

type Config struct {
    Port     int    \`default:"abc"\`  // Invalid integer value
    Timeout  string \`default:"30s"\` // Valid string value
}

cfg := &Config{}
if err := defaults.Set(cfg); err != nil {
    // Error message: error on set Field:[Port], DefaultValue:[abc] Error:[strconv.ParseInt: parsing "abc": invalid syntax]
    fmt.Println(err)
}

Summary

These improvements make the defaults library provide more friendly and detailed error information when encountering configuration errors.

@codecov
Copy link

codecov bot commented Nov 20, 2025

Codecov Report

❌ Patch coverage is 95.52239% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.68%. Comparing base (abebf4b) to head (6c20eed).

Files with missing lines Patch % Lines
defaults.go 95.52% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #59      +/-   ##
==========================================
+ Coverage   95.97%   96.68%   +0.71%     
==========================================
  Files           2        2              
  Lines         149      181      +32     
==========================================
+ Hits          143      175      +32     
  Misses          3        3              
  Partials        3        3              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant