From bf9b0d2558739bfa10c4386ee6fda12c4e8dd2e7 Mon Sep 17 00:00:00 2001 From: dushanlk Date: Sun, 5 Jan 2025 13:46:44 +0530 Subject: [PATCH 1/2] Updated http error handling and responses --- handlers/scrape.go | 18 +++++++++++++++++- handlers/scrape_test.go | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/handlers/scrape.go b/handlers/scrape.go index d9d3a29..b81d2fb 100644 --- a/handlers/scrape.go +++ b/handlers/scrape.go @@ -2,7 +2,9 @@ package handlers import ( "crypto/tls" + "errors" "fmt" + "net" "net/http" "net/url" "strconv" @@ -52,8 +54,22 @@ func ScrapeHandler(context *gin.Context) { pageInfo, err := services.FetchPageInfo(client, baseURL) if err != nil { logger.Error(err) + var netErr net.Error + + if errors.As(err, &netErr) { + if netErr.Timeout() { + context.JSON(http.StatusGatewayTimeout, + utils.BuildErrorResponse("Request timeout during the page fetch")) + return + } else { + context.JSON(http.StatusBadGateway, + utils.BuildErrorResponse("Failed to reach the requested URL")) + return + } + } + context.JSON(http.StatusInternalServerError, - utils.BuildErrorResponse("Failed to fetch page info")) + utils.BuildErrorResponse("An unexpected error occurred")) return } diff --git a/handlers/scrape_test.go b/handlers/scrape_test.go index 423f781..6ba5144 100644 --- a/handlers/scrape_test.go +++ b/handlers/scrape_test.go @@ -2,6 +2,7 @@ package handlers import ( "encoding/json" + "net" "net/http" "net/http/httptest" "scraper/models" @@ -54,7 +55,7 @@ func TestScrapeHandler(test_type *testing.T) { }, }, { - name: "Error Fetching Page Info", + name: "Unexpected Error Fetching Page Info", queryParams: map[string]string{ "url": "http://example.com", }, @@ -63,7 +64,37 @@ func TestScrapeHandler(test_type *testing.T) { mockRequestID: "", expectedStatus: http.StatusInternalServerError, expectedBody: map[string]interface{}{ - "error": "Failed to fetch page info", + "error": "An unexpected error occurred", + }, + }, + { + name: "Timeout Error Fetching Page Info", + queryParams: map[string]string{ + "url": "http://example.com", + }, + mockPageInfo: nil, + mockError: &net.DNSError{ + IsTimeout: true, + }, + mockRequestID: "", + expectedStatus: http.StatusGatewayTimeout, + expectedBody: map[string]interface{}{ + "error": "Request timeout during the page fetch", + }, + }, + { + name: "Failed to Reach The Request URL", + queryParams: map[string]string{ + "url": "http://example.com", + }, + mockPageInfo: nil, + mockError: &net.DNSError{ + IsTimeout: false, + }, + mockRequestID: "", + expectedStatus: http.StatusBadGateway, + expectedBody: map[string]interface{}{ + "error": "Failed to reach the requested URL", }, }, } From 08097b8810c0bcb34e7d38ff639c2dccbe851ccf Mon Sep 17 00:00:00 2001 From: dushanlk Date: Sun, 5 Jan 2025 13:53:58 +0530 Subject: [PATCH 2/2] Updated scrape unit test to cover invalid URL scenario --- handlers/scrape_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/handlers/scrape_test.go b/handlers/scrape_test.go index 6ba5144..5bf0c8c 100644 --- a/handlers/scrape_test.go +++ b/handlers/scrape_test.go @@ -97,6 +97,19 @@ func TestScrapeHandler(test_type *testing.T) { "error": "Failed to reach the requested URL", }, }, + { + name: "Failed to Reach The Request URL", + queryParams: map[string]string{ + "url": "http://example", + }, + mockPageInfo: nil, + mockError: nil, + mockRequestID: "", + expectedStatus: http.StatusBadRequest, + expectedBody: map[string]interface{}{ + "error": "Invalid URL format, please provide a valid URL.", + }, + }, } for _, test_data := range tests {