Skip to content

Commit

Permalink
Addressed review comments: improved error handling and removed redund…
Browse files Browse the repository at this point in the history
…ant recovery blocks
  • Loading branch information
Erfanm83 committed Oct 28, 2024
1 parent 3334258 commit a105080
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
5 changes: 0 additions & 5 deletions httpref.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package httpref

import (
"fmt"
"strings"
)

Expand Down Expand Up @@ -41,10 +40,6 @@ func (r References) InRange(code string) References {
for _, v := range r {
if strings.Contains(v.Name, "-") {
parts := strings.Split(v.Name, "-")
if len(parts) != 2 {
fmt.Printf("Invalid range format for reference name: %s\n", v.Name)
continue
}

start, end := parts[0], parts[1]
if code >= start && code <= end {
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
48 changes: 30 additions & 18 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,42 @@ 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 error occurred: %v\n", rec)
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 {
fmt.Fprintf(os.Stderr, "Failed to update term renderer: %v\n", err)
return "Error rendering description"
return "", err
}
description, err := descriptionStyle.Render(r.Description)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to render description: %v\n", err)
return "Error rendering description"
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 error occurred during renderStyles: %v\n", rec)
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)
}
}()

Expand All @@ -77,9 +81,10 @@ func renderStyles() {
r, err := updateTermRendered(resultStyle)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to update term renderer in renderStyles: %v\n", err)
return
return err
}
descriptionStyle = r
return nil
}

func updateTermRendered(style lipgloss.Style) (*glamour.TermRenderer, error) {
Expand All @@ -99,22 +104,29 @@ func updateTermRendered(style lipgloss.Style) (*glamour.TermRenderer, error) {
func PrintResultsWithStyle(results References, rootStyle lipgloss.Style) {
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "An error occurred during PrintResultsWithStyle: %v\n", rec)
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)
}
}()

switch len(results) {
case 0:
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 a105080

Please sign in to comment.