Skip to content

Commit

Permalink
POC: change error to receive err instead of format
Browse files Browse the repository at this point in the history
  • Loading branch information
vctt94 committed May 27, 2021
1 parent bc4e199 commit fcf3b47
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,7 @@ func (dcr *ExchangeWallet) sendRegFee(addr dcrutil.Address, regFee, netFeeRate u
}
coins, _, _, _, err := dcr.fund(enough)
if err != nil {
return nil, 0, fmt.Errorf("unable to pay registration fee of %s DCR with fee rate of %d atoms/byte: %w",
return nil, 0, fmt.Errorf("Unable to pay registration fee of %s DCR with fee rate of %d atoms/byte: %w",
amount(regFee), netFeeRate, err)
}
return dcr.sendCoins(addr, coins, regFee, netFeeRate, false)
Expand Down
4 changes: 4 additions & 0 deletions client/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (e *Error) Error() string {
return e.s
}

func (e *Error) Code() int {
return e.code
}

// newError is a constructor for a new Error.
func newError(code int, s string, a ...interface{}) error {
return &Error{
Expand Down
21 changes: 20 additions & 1 deletion client/webserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package webserver

import (
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (s *WebServer) apiRegister(w http.ResponseWriter, r *http.Request) {
Fee: reg.Fee,
})
if err != nil {
s.writeAPIError(w, "registration error: %v", err)
s.writeAPIErrorPOC(w, err)
return
}
// There was no error paying the fee, but we must wait on confirmations
Expand Down Expand Up @@ -690,3 +691,21 @@ func (s *WebServer) writeAPIError(w http.ResponseWriter, format string, a ...int
}
writeJSON(w, resp, s.indent)
}

// writeAPIError logs the formatted error and sends a standardResponse with the
// error message.
func (s *WebServer) writeAPIErrorPOC(w http.ResponseWriter, err error) {
// log.Error(errMsg)
var cErr *core.Error
var code int
if errors.As(err, &cErr) {
code = cErr.Code()
}
resp := &standardResponse{
OK: false,
Msg: err.Error(),
Code: code,
}
log.Error(err.Error())
writeJSON(w, resp, s.indent)
}
13 changes: 13 additions & 0 deletions client/webserver/site/src/html/register.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@
</div>
</form>

{{- /* registration failed low balance. */ -}}
<form class="card mx-auto my-5 bg1 d-hide" id="failedRegForm">
<div class="bg2 px-2 py-1 text-center fs18">Insuficient funds</div>
<div class="p-4">
<div class="fs16">
Looks like you do not have enough funds for paying the fee in the
selected account. Please fund the account and try again.
</div>
<div class="fs15 pt-3 text-center d-hide errcolor" id="regFundsErr"></div>
<hr class="dashed mt-4">
</div>
</form>

</div>
</div>
{{template "bottom"}}
Expand Down
17 changes: 15 additions & 2 deletions client/webserver/site/src/js/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { NewWalletForm, bindOpenWallet, bind as bindForm } from './forms'
const DCR_ID = 42
const animationLength = 300

// XXX: move code errors to a specific file?
const sendFeeErr = 8;

let app

export default class RegistrationPage extends BasePage {
Expand All @@ -27,7 +30,7 @@ export default class RegistrationPage extends BasePage {
'dexShowMore',
// Form 5: Confirm DEX registration and pay fee
'confirmRegForm', 'feeDisplay', 'dexDCRLotSize', 'appPass', 'submitConfirm', 'regErr',
'dexCertBox'
'dexCertBox', 'failedRegForm', 'regFundsErr'
])

// Hide the form closers for the registration process.
Expand Down Expand Up @@ -86,7 +89,9 @@ export default class RegistrationPage extends BasePage {
form1.style.right = '0'
form2.style.right = -shift
Doc.show(form2)
form2.querySelector('input').focus()
if (form2.querySelector('input')) {
form2.querySelector('input').focus()
}
await Doc.animate(animationLength, progress => {
form2.style.right = `${-shift + progress * shift}px`
}, 'easeOutHard')
Expand Down Expand Up @@ -189,6 +194,14 @@ export default class RegistrationPage extends BasePage {
const res = await postJSON('/api/register', registration)
loaded()
if (!app.checkResponse(res)) {
// show different form with no passphrase input in case of no funds.
if (res.code === sendFeeErr) {
await this.changeForm(page.confirmRegForm, page.failedRegForm)
page.regFundsErr.textContent = res.msg
Doc.show(page.regFundsErr)
return
}

page.regErr.textContent = res.msg
Doc.show(page.regErr)
return
Expand Down
5 changes: 3 additions & 2 deletions client/webserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (

// standardResponse is a basic API response when no data needs to be returned.
type standardResponse struct {
OK bool `json:"ok"`
Msg string `json:"msg,omitempty"`
OK bool `json:"ok"`
Msg string `json:"msg,omitempty"`
Code int `json:"code,omitempty"`
}

// simpleAck is a plain standardResponse with "ok" = true.
Expand Down

0 comments on commit fcf3b47

Please sign in to comment.