-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add debug flag and refactor error handling
Introduce a new --debug flag to provide more detailed error messages Change the program to return exit code 1 when an error occurs Refactor error handling for better readability and maintainability Update test cases to reflect changes
- Loading branch information
nvima
committed
Apr 23, 2023
1 parent
d2e26b0
commit 0b4eb99
Showing
7 changed files
with
92 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package main | ||
|
||
import "github.com/nvima/httpcli/cmd" | ||
import ( | ||
"os" | ||
|
||
"github.com/nvima/httpcli/cmd" | ||
) | ||
|
||
func main() { | ||
cmd.Execute() | ||
err := cmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,21 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
) | ||
|
||
type TplError struct { | ||
msg string | ||
err error | ||
} | ||
|
||
func (te TplError) Err() error { | ||
return fmt.Errorf(te.msg) | ||
} | ||
|
||
func (te TplError) Msg() string { | ||
return te.msg | ||
} | ||
|
||
var NO_CONFIG_FILE_ERR = TplError{ | ||
msg: "No config file found", | ||
} | ||
|
||
var NO_FUNC_NAME_ERR = TplError{ | ||
msg: "No function name provided", | ||
} | ||
var NO_FUNC_FOUND_ERR = TplError{ | ||
msg: "Function not found in config", | ||
} | ||
|
||
var INVALID_CONFIG_ERR = TplError{ | ||
msg: "Invalid config file", | ||
} | ||
|
||
var INVALID_RESP_CODE = TplError{ | ||
msg: "Invalid response code", | ||
} | ||
var FAILED_TO_GET_DATA = TplError{ | ||
msg: "Failed to get data", | ||
} | ||
var FAILED_TO_MAKE_HTTP_CALL = TplError{ | ||
msg: "Failed to make http call", | ||
} | ||
var FAILED_TO_PARSE_JSON = TplError{ | ||
msg: "Failed to parse JSON", | ||
} | ||
var FAILED_TO_PARSE_OUTPUT_FIELD = TplError{ | ||
msg: "Failed to parse output field", | ||
} | ||
var NO_URL_ERR = TplError{ | ||
msg: "No URL provided", | ||
} | ||
var NO_CONFIG_FILE_ERR = errors.New("No config file found") | ||
var NO_FUNC_NAME_ERR = errors.New("No function name provided") | ||
var NO_FUNC_FOUND_ERR = errors.New("Function not found in config") | ||
var INVALID_CONFIG_ERR = errors.New("Invalid config file") | ||
var INVALID_RESP_CODE = errors.New("Invalid response code") | ||
var FAILED_TO_GET_DATA = errors.New("Failed to get data") | ||
var FAILED_TO_MAKE_HTTP_CALL = errors.New("Failed to make http call") | ||
var FAILED_TO_PARSE_JSON_RESPONSE = errors.New("Failed to parse JSON Response") | ||
var FAILED_TO_PARSE_OUTPUT_FIELD = errors.New("Failed to parse output field") | ||
var NO_URL_ERR = errors.New("No URL provided") | ||
var MARSHAL_DATA_FAILED = errors.New("Failed to marshal data") | ||
var REPLACE_STDIN_FAILED = errors.New("Failed to replace stdin") | ||
var INIT_HTTP_POST_REQUEST_FAILED = errors.New("An error occurred while initializing the HTTP POST request. Possible causes could be an invalid URL or incorrect input parameters.") | ||
var SEND_HTTP_POST_REQUEST_FAILED = errors.New("An error occurred while sending the HTTP POST request. Possible causes could be network issues, server unavailability, or issues with the request payload.") | ||
var READ_RESPONSE_BODY_FAILED = errors.New("Failed to read the response body") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package util | ||
|
||
import ( | ||
// "fmt" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func HandleError(cmd *cobra.Command, err error, tplError TplError) error { | ||
//TODO:: Add Error Logging with Stacktraces | ||
// fmt.Println(err) | ||
|
||
// fmt.Fprintf(cmd.OutOrStdout(), tplError.msg) | ||
return err | ||
func HandleError(cmd *cobra.Command, err error, tplError error) error { | ||
debug, _ := cmd.Flags().GetBool("debug") | ||
if debug { | ||
fmt.Printf("Debug: %v\n", err) | ||
} | ||
return tplError | ||
} |