Skip to content

Commit

Permalink
Merge pull request #50 from Erfanm83/improve-error-handling
Browse files Browse the repository at this point in the history
Improved error handling for reference methods
  • Loading branch information
dnnrly authored Nov 12, 2024
2 parents 66b47ac + a105080 commit 744579b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
5 changes: 4 additions & 1 deletion httpref.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func (r References) InRange(code string) References {
for _, v := range r {
if strings.Contains(v.Name, "-") {
parts := strings.Split(v.Name, "-")
if code >= parts[0] && code <= parts[len(parts)-1] {

start, end := parts[0], parts[1]
if code >= start && code <= end {
return References{v}
}
}
Expand All @@ -62,6 +64,7 @@ func (r References) Titles() References {
return results
}

// Search looks for references that contain the search term in their fields
func (r References) Search(term string) References {
results := References{}

Expand Down
19 changes: 16 additions & 3 deletions httpref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,21 @@ func TestReferences_InRange(t *testing.T) {
})
}

t.Run("Invalid port should not return anything", func(t *testing.T) {
t.Run("Invalid port should not return anything", func(t *testing.T) {
got := RegisteredPorts.InRange("70000") // Invalid port, should not return anything
if len(got) != 0 {
t.Errorf("References.InRange() = %v, want %v", len(got), 0)
}
})

t.Run("Malformed range should not return anything", func(t *testing.T) {
// This tests the scenario where the reference has a malformed range format
RegisteredPorts = append(RegisteredPorts, Reference{Name: "invalid-range"})
got := RegisteredPorts.InRange("1000")
if len(got) != 0 {
t.Errorf("References.InRange() with malformed range = %v, want %v", len(got), 0)
}
})
}

func TestReference_SummarizeContainsFormattedTitle(t *testing.T) {
Expand Down Expand Up @@ -92,8 +101,9 @@ func TestReference_SummarizeContainsCorrectSummary(t *testing.T) {

func TestReference_DescribeLooksUpExpectedData(t *testing.T) {
r := Headers.ByName("Headers")[0]
description := r.Describe(lipgloss.NewStyle().Width(100))
description, err := r.Describe(lipgloss.NewStyle().Width(100))

assert.NoError(t, err)
assert.Contains(t, description, "HTTP")
assert.Contains(t, description, "apply")
}
Expand All @@ -107,8 +117,11 @@ func TestPortsConsistencyValidation(t *testing.T) {
ports := append(WellKnownPorts[1:], RegisteredPorts[1:]...)
var validRange = regexp.MustCompile(`^\d+(-\d+)?$`)
for _, port := range ports {
if port.Name == "invalid-range" {
continue // Skip known invalid range for test purposes
}
if !validRange.MatchString(port.Name) {
t.Errorf("Invalid port format: %v", port.Name)
assert.Fail(t, "Invalid port format", "Port name '%s' does not match valid range format", port.Name)
}
}
}
Expand Down
66 changes: 49 additions & 17 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,45 @@ func (r Reference) Summarize(style lipgloss.Style) string {
return lipgloss.JoinVertical(lipgloss.Bottom, name, summary, statusBar)
}

// Describe creates a full, formated description of a reference
func (r Reference) Describe(style lipgloss.Style) string {
// Describe creates a full, formatted description of a reference
func (r Reference) Describe(style lipgloss.Style) (string, error) {
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "An unexpected error occurred: %v\nPlease raise an issue on GitHub: https://github.com/Erfanm83/httpref/issues/new\n", rec)
}
}()

statusBar := renderStatusBar(r.Name, r.Summary)
descriptionStyle, err := updateTermRendered(style)
if err != nil {
// hoping this doesn't happen as most commands here suceed without issue
panic(err)
fmt.Fprintf(os.Stderr, "Failed to update term renderer: %v\n", err)
return "", err
}
description, err := descriptionStyle.Render(r.Description)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "Failed to render description: %v\n", err)
return "", err
}
descriptionWithBorder := descriptionBorderStyle.Render(description)

return lipgloss.JoinVertical(lipgloss.Bottom, statusBar, descriptionWithBorder)
return lipgloss.JoinVertical(lipgloss.Bottom, statusBar, descriptionWithBorder), nil
}

func init() {
renderStyles()
err := renderStyles()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize render styles: %v\nPlease raise an issue on GitHub: https://github.com/dnnrly/httpref/issues/new \n", err)
os.Exit(1)
}
}

func renderStyles() {
func renderStyles() error {
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "An unexpected error occurred during renderStyles: %v\nPlease raise an issue on GitHub: https://github.com/dnnrly/httpref/issues/new \n", rec)
}
}()

resultStyle := lipgloss.NewStyle()
descriptionForeColorDarkTheme := "120"
descriptionForeColorLightTheme := "202"
Expand All @@ -63,10 +80,11 @@ func renderStyles() {

r, err := updateTermRendered(resultStyle)
if err != nil {
// hoping this doesn't happen as most commands here suceed without issue
panic(err)
fmt.Fprintf(os.Stderr, "Failed to update term renderer in renderStyles: %v\n", err)
return err
}
descriptionStyle = r
return nil
}

func updateTermRendered(style lipgloss.Style) (*glamour.TermRenderer, error) {
Expand All @@ -84,17 +102,31 @@ func updateTermRendered(style lipgloss.Style) (*glamour.TermRenderer, error) {
}

func PrintResultsWithStyle(results References, rootStyle lipgloss.Style) {
switch len(results) {
case 0:
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "An unexpected error occurred during PrintResultsWithStyle: %v\nPlease raise an issue on GitHub: https://github.com/Erfanm83/httpref/issues/new\n", rec)
os.Exit(1)
}
}()

if len(results) == 0 {
res := "Filter not found any results\n"
fmt.Fprintf(os.Stderr, res)
os.Exit(1)
case 1:
fmt.Printf("%s\n", results[0].Describe(rootStyle))
default:
for _, r := range results {
fmt.Printf("%s\n", r.Summarize(rootStyle))
}

if len(results) == 1 {
description, err := results[0].Describe(rootStyle)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to describe reference: %v\n", err)
os.Exit(1)
}
fmt.Printf("%s\n", description)
return
}

for _, r := range results {
fmt.Printf("%s\n", r.Summarize(rootStyle))
}
}

Expand Down

0 comments on commit 744579b

Please sign in to comment.