map_nip47_error.go raw

   1  package controllers
   2  
   3  import (
   4  	"errors"
   5  
   6  	"github.com/getAlby/hub/constants"
   7  	"github.com/getAlby/hub/nip47/models"
   8  	"github.com/getAlby/hub/transactions"
   9  	"gorm.io/gorm"
  10  )
  11  
  12  func mapNip47Error(err error) *models.Error {
  13  	code := constants.ERROR_INTERNAL
  14  	message := err.Error()
  15  	if errors.Is(err, transactions.NewNotFoundError()) {
  16  		code = constants.ERROR_NOT_FOUND
  17  	}
  18  	if errors.Is(err, transactions.NewInsufficientBalanceError()) {
  19  		code = constants.ERROR_INSUFFICIENT_BALANCE
  20  	}
  21  	if errors.Is(err, transactions.NewQuotaExceededError()) {
  22  		code = constants.ERROR_QUOTA_EXCEEDED
  23  	}
  24  	if errors.Is(err, gorm.ErrDuplicatedKey) {
  25  		// avoid leaking raw driver/SQL error details (e.g. constraint names) to NWC apps
  26  		message = gorm.ErrDuplicatedKey.Error()
  27  	}
  28  
  29  	return &models.Error{
  30  		Code:    code,
  31  		Message: message,
  32  	}
  33  }
  34