How to Handles payment gateway failures error in Magento 2?

Every eCommerce store has integrated multiple payment gateways based on its region’s most used payment provider. The payment entity is the key component for every online order placing. Gateway is offline or online.

When developing a custom payment method, We have to handle failure error during transactions,  Handling gateway failures error in your code level improves the customer experience in your site.

Magento uses Magento\Sales\Api\PaymentFailuresInterface for managing payment gateway failures with handle() method to handle payment failure for the current order.

<?php
namespace Jesadiya\PaymentFailure\Model;

use Exception;
use Magento\Sales\Api\PaymentFailuresInterface;

class PaymentFailure
{
    /**
     * @var PaymentFailuresInterface
     */
    private $paymentFailures;

    public function __construct(
        PaymentFailuresInterface $paymentFailures
    ) {
        $this->paymentFailures = $paymentFailures;
    }

    public function placeOrder(\Magento\Sales\Api\Data\OrderInterface $order)
    {
        try {
            $order->place();
        } catch (Exception $exception) {
            $this->paymentFailures->handle(
                (int)$order->getQuoteId(),
                __($exception->getMessage()));
            throw $exception;
        }
    }
}

I have just given an example to handle payment failure for the order.

$this->paymentFailures->handle(
    (int)$order->getQuoteId(),
    __($errorMessage),
    $checkoutType = 'onepage'
);

The first Parameter is ordered cart(quote) Id.
The second parameter is for the error message.
The third parameter is the checkout type. the default value is onepage.