Skip to content

[Feature]: Implement Logging and Structured Error Handling of the Payment Gateway system #37

@adityacosmos24

Description

@adityacosmos24

So, what is it about?


Title: Implement Logging and Structured Error Handling

Issue Description

Current Behavior 📝

The current implementation of the Payment Gateway system directly prints transaction status updates to the standard output (std::cout). While this is useful for real-time monitoring during a run, it lacks persistence and structure. If the application closes or crashes, all transaction history is lost.

Furthermore, error handling is managed through simple boolean return types. When a step like validatePayment or initiatePayment fails, the system only knows that it failed, but not why. For example, the PaytmGateway::validatePayment function returns false if the amount is invalid or if the currency is not "INR", but the calling function cannot distinguish between these two different error conditions.

Expected Behavior ✅

To make the system more robust and production-ready, two key enhancements are needed:

  1. File-Based Logging: All transaction events should be logged to a file (e.g., payment_gateway.log). Each log entry should be timestamped and should clearly describe the event (e.g., validation start, initiation failure, retry attempt, final success/failure). This creates a persistent audit trail for debugging and analysis.

  2. Structured Error Handling: Instead of returning simple booleans, the payment processing functions should provide specific details about errors. This can be achieved by:

    • Throwing custom exceptions (e.g., ValidationException, InitiationException).
    • Returning a struct or std::pair that contains both a success status and an error message string.

This will allow the system to log meaningful error messages, such as "Validation failed: Currency must be INR" instead of a generic "[PaymentGateway] Validation failed".

Motivation for Change 💡

Implementing logging and structured error handling are crucial steps in moving this project from a proof-of-concept to a more mature and maintainable application. These changes will:

  • Improve Debugging: A persistent log file makes it possible to trace the lifecycle of a transaction and diagnose issues after the fact.
  • Enhance Maintainability: Specific error messages make it easier for developers to understand and fix bugs.
  • Provide Auditing Capabilities: Logs serve as a record of all payment activities, which is essential for any financial application.

Suggested Implementation 🛠️

  1. Create a Logger Class: Introduce a singleton Logger class that can write formatted messages to a file.

    class Logger {
    public:
        static Logger& getInstance();
        void log(const std::string& message);
    private:
        // Private constructor, destructor, etc.
        // std::ofstream file_handle;
    };
  2. Integrate Logging: Call the Logger from within the PaymentGateway, BankingSystem, and PaymentGatewayProxy classes to record every significant step of the payment process.

  3. Refactor Error Handling: Modify the methods in the PaymentGateway hierarchy. For example, change validatePayment to throw an exception on failure.

    // In PaytmGateway.h
    class ValidationException : public std::exception {
    public:
        const char* what() const noexcept override {
            return message.c_str();
        }
    private:
        std::string message;
    };
    
    // In PaytmGateway.cpp
    bool PaytmGateway::validatePayment(PaymentRequest* request) {
        // ...
        if (request->currency != "INR") {
            throw ValidationException("Invalid currency. Must be INR.");
        }
        // ...
        return true;
    }
  4. Add Exception Handling: Update the PaymentGateway::processPayment template method and the PaymentController to include try...catch blocks to handle these exceptions gracefully and log the specific error messages.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions