Merge pull request 'Report Lndhub API errors to Sentry' (#133) from refactor/lndhub_integration into master
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #133
This commit is contained in:
Râu Cao 2023-06-06 15:44:36 +00:00
commit de45d070aa
4 changed files with 21 additions and 72 deletions

View File

@ -37,8 +37,8 @@ class Services::LightningController < ApplicationController
session[:ln_auth_token] = auth_token session[:ln_auth_token] = auth_token
@ln_auth_token = auth_token @ln_auth_token = auth_token
end end
rescue rescue => e
# TODO add exception tracking Sentry.capture_exception(e) if Setting.sentry_enabled?
end end
def set_current_section def set_current_section
@ -49,9 +49,9 @@ class Services::LightningController < ApplicationController
lndhub = Lndhub.new lndhub = Lndhub.new
data = lndhub.balance @ln_auth_token data = lndhub.balance @ln_auth_token
@balance = data["BTC"]["AvailableBalance"] rescue nil @balance = data["BTC"]["AvailableBalance"] rescue nil
rescue rescue AuthError
authenticate_with_lndhub(force_reauth: true) authenticate_with_lndhub(force_reauth: true)
return nil if @fetch_balance_retried raise if @fetch_balance_retried
@fetch_balance_retried = true @fetch_balance_retried = true
fetch_balance fetch_balance
end end
@ -61,9 +61,9 @@ class Services::LightningController < ApplicationController
txs = lndhub.gettxs @ln_auth_token txs = lndhub.gettxs @ln_auth_token
invoices = lndhub.getuserinvoices(@ln_auth_token).select{|i| i["ispaid"]} invoices = lndhub.getuserinvoices(@ln_auth_token).select{|i| i["ispaid"]}
process_transactions(txs + invoices) process_transactions(txs + invoices)
rescue rescue AuthError
authenticate_with_lndhub(force_reauth: true) authenticate_with_lndhub(force_reauth: true)
return [] if @fetch_transactions_retried raise if @fetch_transactions_retried
@fetch_transactions_retried = true @fetch_transactions_retried = true
fetch_transactions fetch_transactions
end end

1
app/errors/auth_error.rb Normal file
View File

@ -0,0 +1 @@
class AuthError < StandardError; end

View File

@ -12,12 +12,7 @@ class Lndhub
end end
res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers
log_error(res) if res.status != 200
if res.status != 200
Rails.logger.error "[lndhub] API request failed:"
Rails.logger.error res.body
#TODO add some kind of exception tracking/notifications
end
JSON.parse(res.body) JSON.parse(res.body)
end end
@ -31,7 +26,7 @@ class Lndhub
data = JSON.parse(res.body) data = JSON.parse(res.body)
if data.is_a?(Hash) && data["error"] && data["message"] == "bad auth" if data.is_a?(Hash) && data["error"] && data["message"] == "bad auth"
raise "BAD_AUTH" raise AuthError
else else
data data
end end
@ -68,4 +63,13 @@ class Lndhub
invoice["payment_request"] invoice["payment_request"]
end end
def log_error(res)
Rails.logger.error "[lndhub] API request failed:"
Rails.logger.error res.body
if Setting.sentry_enabled?
Sentry.capture_message("Lndhub API request failed: #{res.body}")
end
end
end end

View File

@ -1,9 +1,4 @@
class LndhubV2 class LndhubV2 < Lndhub
attr_accessor :auth_token
def initialize
@base_url = ENV["LNDHUB_API_URL"]
end
def post(endpoint, payload, options={}) def post(endpoint, payload, options={})
headers = { "Content-Type" => "application/json" } headers = { "Content-Type" => "application/json" }
@ -12,64 +7,12 @@ class LndhubV2
elsif options[:admin_token] elsif options[:admin_token]
headers.merge!({ "Authorization" => "Bearer #{options[:admin_token]}" }) headers.merge!({ "Authorization" => "Bearer #{options[:admin_token]}" })
end end
res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers
log_error(res) if res.status != 200
if res.status != 200
Rails.logger.error "[lndhub] API request failed:"
Rails.logger.error res.body
#TODO add some kind of exception tracking/notifications
end
JSON.parse(res.body) JSON.parse(res.body)
end end
def get(endpoint, auth_token)
res = Faraday.get("#{@base_url}/#{endpoint}", {}, {
"Content-Type" => "application/json",
"Accept" => "application/json",
"Authorization" => "Bearer #{auth_token}"
})
JSON.parse(res.body)
end
def create(payload)
post "create", payload
end
def authenticate(user)
credentials = post "auth?type=auth", { login: user.ln_account, password: user.ln_password }
self.auth_token = credentials["access_token"]
self.auth_token
end
def balance(user_token=nil)
get "balance", user_token || auth_token
end
def gettxs(user_token)
get "gettxs", user_token || auth_token
end
def getuserinvoices(user_token)
get "getuserinvoices", user_token || auth_token
end
def addinvoice(payload)
invoice = post "addinvoice", {
amt: payload[:amount],
memo: payload[:memo],
description_hash: payload[:description_hash]
}
invoice["payment_request"]
end
#
# V2
#
def create_account(payload={}) def create_account(payload={})
post "v2/users", payload, admin_token: Rails.application.credentials.lndhub[:admin_token] post "v2/users", payload, admin_token: Rails.application.credentials.lndhub[:admin_token]
end end
@ -78,4 +21,5 @@ class LndhubV2
# Payload: { amount: 1000, description: "", description_hash: "" } # Payload: { amount: 1000, description: "", description_hash: "" }
post "v2/invoices", payload post "v2/invoices", payload
end end
end end