Skip to content

Commit 5a74716

Browse files
committed
fix: valid JSON requires the strings be quoted
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 44b4b0b commit 5a74716

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

internal/api/api.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func handleSubmitTx(c *gin.Context) {
137137
if c.ContentType() != "application/cbor" {
138138
// Log the error, return an error to the user, and increment failed count
139139
logger.Errorf("invalid request body, should be application/cbor")
140-
c.Data(415, "application/json", []byte("invalid request body, should be application/cbor"))
140+
c.JSON(415, "invalid request body, should be application/cbor")
141141
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
142142
return
143143
}
@@ -146,7 +146,7 @@ func handleSubmitTx(c *gin.Context) {
146146
if err != nil {
147147
// Log the error, return an error to the user, and increment failed count
148148
logger.Errorf("failed to read request body: %s", err)
149-
c.Data(500, "application/json", []byte("failed to read request body"))
149+
c.JSON(500, "failed to read request body")
150150
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
151151
return
152152
}
@@ -158,7 +158,7 @@ func handleSubmitTx(c *gin.Context) {
158158
var txUnwrap []cbor.RawMessage
159159
if err := cbor.Unmarshal(txRawBytes, &txUnwrap); err != nil {
160160
logger.Errorf("failed to unwrap transaction CBOR: %s", err)
161-
c.Data(400, "application/json", []byte(fmt.Sprintf("failed to unwrap transaction CBOR: %s", err)))
161+
c.JSON(400, fmt.Sprintf("failed to unwrap transaction CBOR: %s", err))
162162
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
163163
return
164164
}
@@ -187,21 +187,21 @@ func handleSubmitTx(c *gin.Context) {
187187
}()
188188
if err != nil {
189189
logger.Errorf("failure creating Ouroboros connection: %s", err)
190-
c.Data(500, "application/json", []byte("failure communicating with node"))
190+
c.JSON(500, "failure communicating with node")
191191
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
192192
return
193193
}
194194
if cfg.Node.Address != "" && cfg.Node.Port > 0 {
195195
if err := oConn.Dial("tcp", fmt.Sprintf("%s:%d", cfg.Node.Address, cfg.Node.Port)); err != nil {
196196
logger.Errorf("failure connecting to node via TCP: %s", err)
197-
c.Data(500, "application/json", []byte("failure communicating with node"))
197+
c.JSON(500, "failure communicating with node")
198198
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
199199
return
200200
}
201201
} else {
202202
if err := oConn.Dial("unix", cfg.Node.SocketPath); err != nil {
203203
logger.Errorf("failure connecting to node via UNIX socket: %s", err)
204-
c.Data(500, "application/json", []byte("failure communicating with node"))
204+
c.JSON(500, "failure communicating with node")
205205
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
206206
return
207207
}
@@ -211,7 +211,7 @@ func handleSubmitTx(c *gin.Context) {
211211
err, ok := <-errorChan
212212
if ok {
213213
logger.Errorf("failure communicating with node: %s", err)
214-
c.Data(500, "application/json", []byte("failure communicating with node"))
214+
c.JSON(500, "failure communicating with node")
215215
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil)
216216
doneChan <- true
217217
}
@@ -220,7 +220,7 @@ func handleSubmitTx(c *gin.Context) {
220220
localTxSubmissionCallbackConfig := &localtxsubmission.CallbackConfig{
221221
AcceptTxFunc: func() error {
222222
// Return transaction ID
223-
c.Data(202, "application/json", []byte(txIdHex))
223+
c.JSON(202, txIdHex)
224224
doneChan <- true
225225
// Increment custom metric
226226
_ = ginmetrics.GetMonitor().GetMetric("tx_submit_count").Inc(nil)
@@ -229,9 +229,9 @@ func handleSubmitTx(c *gin.Context) {
229229
RejectTxFunc: func(reasonCbor []byte) error {
230230
var reason interface{}
231231
if err := cbor.Unmarshal(reasonCbor, &reason); err == nil {
232-
c.Data(400, "application/json", []byte(fmt.Sprintf("transaction rejected by node: %v (raw CBOR: %x)", reason, reasonCbor)))
232+
c.JSON(400, fmt.Sprintf("transaction rejected by node: %v (raw CBOR: %x)", reason, reasonCbor))
233233
} else {
234-
c.Data(400, "application/json", []byte(fmt.Sprintf("transaction rejected by node, but the 'reason' data could not be parsed (raw CBOR: %x)", reasonCbor)))
234+
c.JSON(400, fmt.Sprintf("transaction rejected by node, but the 'reason' data could not be parsed (raw CBOR: %x)", reasonCbor))
235235
}
236236
doneChan <- true
237237
// Increment custom metric
@@ -243,13 +243,13 @@ func handleSubmitTx(c *gin.Context) {
243243
// Determine transaction type (era)
244244
txType, err := determineTransactionType(txRawBytes)
245245
if err != nil {
246-
c.Data(400, "application/json", []byte("could not parse transaction to determine type"))
246+
c.JSON(400, "could not parse transaction to determine type")
247247
return
248248
}
249249
// Submit the transaction
250250
if err := oConn.LocalTxSubmission.SubmitTx(txType, txRawBytes); err != nil {
251251
logger.Errorf("failure submitting transaction: %s", err)
252-
c.Data(500, "application/json", []byte("failure communicating with node"))
252+
c.JSON(500, "failure communicating with node")
253253
return
254254
}
255255
// Wait for async process to finish

0 commit comments

Comments
 (0)