Hone logo
Hone
Problems

Crafting Custom Errors in Go

Go's error handling mechanism is a cornerstone of robust applications. This challenge focuses on creating and utilizing custom error types, allowing for more specific and informative error reporting. Understanding how to define and propagate custom errors is crucial for building maintainable and debuggable Go programs.

Problem Description

You are tasked with creating a custom error type in Go to represent a specific scenario within a simplified file processing application. The application attempts to read a file and process its contents. If the file doesn't exist, or if the file's content is invalid (e.g., contains non-numeric characters when expecting numbers), a custom error should be returned.

Specifically, you need to:

  1. Define a custom error type named FileProcessingError. This type should be a struct containing a Message field (string) and a Filename field (string).
  2. Create a function ProcessFile(filename string) (int, error) that attempts to read the file specified by filename.
    • If the file does not exist, return a FileProcessingError with an appropriate message and the filename.
    • If the file exists, read its contents. Assume the file contains a single integer, represented as a string.
    • If the file content is not a valid integer, return a FileProcessingError with an appropriate message and the filename.
    • If the file exists and contains a valid integer, parse the integer and return it along with nil error.
  3. Write a main function that calls ProcessFile with a sample filename. Handle the returned error, printing the error message and filename if an error occurred. If no error occurred, print the parsed integer.

Examples

Example 1:

Input: filename = "nonexistent_file.txt"
Output: File processing error: nonexistent_file.txt

Explanation: The file "nonexistent_file.txt" does not exist, so a FileProcessingError is returned with the appropriate message and filename.

Example 2:

Input: filename = "invalid_file.txt" (content: "abc")
Output: File processing error: invalid_file.txt: invalid integer format

Explanation: The file "invalid_file.txt" exists, but its content "abc" is not a valid integer, so a FileProcessingError is returned with the appropriate message and filename.

Example 3:

Input: filename = "valid_file.txt" (content: "123")
Output: 123

Explanation: The file "valid_file.txt" exists and contains the valid integer "123", so the integer 123 is returned, and the error is nil.

Constraints

  • The ProcessFile function must return an integer and an error.
  • The custom error type FileProcessingError must be defined as a struct with Message and Filename fields.
  • Error messages should be descriptive and include the filename.
  • Assume the file exists if os.Stat returns nil error.
  • Assume the file is readable if os.Open returns nil error.
  • The main function should handle the error returned by ProcessFile gracefully.

Notes

  • You'll need to use the os package to interact with the file system.
  • Consider using strconv.Atoi to convert the file content to an integer.
  • Think about how to create instances of your custom error type with meaningful messages.
  • The Error() method is implicitly implemented for structs, so you don't need to define it explicitly. Go automatically uses the struct's string representation when an error interface is expected.
  • For testing purposes, you can create temporary files using os.Create and write content to them. Remember to clean up these files after testing.
Loading editor...
go