Skip to content

A Go module demonstrating robust error handling using sentinel errors, error wrapping, context timeouts, and custom error types. Includes a sample task runner that reads tasks from a JSON file and processes them.

License

Notifications You must be signed in to change notification settings

0mynk/task_processor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Task Runner

A simple Go project to demonstrate robust error handling patterns including:

  • ✅ Sentinel errors
  • 🔄 Error wrapping with %w
  • 🧩 Custom error types
  • ⏳ Context-based timeouts
  • 🔍 Error inspection with errors.Is and errors.As

Overview

The taskrunner simulates a basic task execution system. It reads a list of tasks from a JSON file, validates them, and processes each task within a deadline. This project is designed as a practical guide to idiomatic error handling in Go.


Project Structure


taskrunner/
├── go.mod
├── main.go
├── README.md
├── taskrunner
│   ├── processor.go
│   ├── reader.go
│   └── task.go
└── tasks.json

Getting Started

Prerequisites

  • Go 1.18 or higher installed

Clone & Run

git clone https://github.com/0mynk/task_processor
cd task_processor
go mod tidy  #optional
go run main.go

Sample Input: tasks.json

[
    { "id": "task1", "value": 42 },
    { "id": "task2", "value": -5 },
    { "id": "task3", "value": 13 },
    { "id": "task4", "value": 42 },
    { "id": "task5", "value": 3 },
    { "id": "task6", "value": 1 },
    { "id": "task7", "value": 2 },
    { "id": "task8", "value": 22 }
]

What You'll Learn

Feature Description
Sentinel Errors Reusable, predefined error values like ErrFileNotFound, checked with errors.Is
Error Wrapping Use %w in fmt.Errorf to preserve and trace error origins
Custom Error Types Struct-based errors (ErrInvalidTask) to carry task-specific info
Context Cancellation Timeout each task using context.WithTimeout
Error Unwrapping Detect specific error types with errors.As and errors.Is

Example Output

Task "task1" timed out
Skipping invalid task "task2": negative value
Task "task3" timed out
Task "task4" timed out
Task "task5" completed successfully
Task "task6" completed successfully
Task "task7" completed successfully
Task "task8" timed out

💡 Extend This Project

  • Add unit tests with the testing package
  • Implement concurrency with goroutines and channels
  • Add retries or backoff mechanisms
  • Log errors using a structured logger like zap or logrus

License

MIT License — free to use, modify, and share.

Author

Crafted as a learning tool for mastering Go's error handling. Feel free to fork, star, or open a pull request!

About

A Go module demonstrating robust error handling using sentinel errors, error wrapping, context timeouts, and custom error types. Includes a sample task runner that reads tasks from a JSON file and processes them.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages