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.

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 {
        } catch (Exception $exception) {
            throw $exception;

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

    $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.