Ao forçar o erro no json, passando mais caracteres do que o permitido para salvar no banco, ao invés de me retornar o erro 400, o servidor está sendo derrubado. Não consegui obter o mesmo comportamento do video. Quando o JSON está ok, salva certinho.
ConnectionFactory.js
module.exports = function(app) {
app.get('/payments', function(req, res) {
console.log('Received Test request on port 3000');
res.send("OK.");
});
app.post('/payments/payment', function(req, res){
var payment = req.body;
console.log('processing a new payment request...');
var connection = app.db.connectionFactory();
var paymentDAO = new app.db.PaymentDAO(connection);
payment.status = "CREATED";
payment.date = new Date;
paymentDAO.save(payment, function(error, result){
if (error) {
console.log("error while trying to persist on DB..." + erro)
res.status(400).send(error);
} else {
console.log('payment created: ' + result);
res.json(payment);
}
});
});
}
payment.json
{
"payment_type" : "payfast",
"value" : 22.50,
"currency" : "BRLBRL",
"description" : "Creating a payment"
}
CURL:
curl http://localhost:3000/payments/payment -X POST -v -H "Content-type: application/json" -d @files/payment.json | json_pp
O resultado que estou obtendo é o:
studentlevel@MacBook-Pro-de-Sergio:$ curl http://localhost:3000/payments/payment -X POST -v -H "Content-type: application/json" -d @files/payment.json | json_pp
Note: Unnecessary use of -X or --request, POST is already inferred.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /payments/payment HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.51.0
> Accept: */*
> Content-type: application/json
> Content-Length: 105
>
} [105 bytes data]
* upload completely sent off: 105 out of 105 bytes
100 105 0 0 100 105 0 1342 --:--:-- --:--:-- --:--:-- 1329* Curl_http_done: called premature == 0
* Empty reply from server
100 105 0 0 100 105 0 1338 --:--:-- --:--:-- --:--:-- 1329
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") at /usr/bin/json_pp5.18 line 45.
e o server:
Server running on port 3000.
processing a new payment request...
error while trying to persist on DB...
/Users/studentlevel/My GitHub Repositories/payfast/node_modules/mysql/lib/protocol/Parser.js:79
throw err; // Rethrow non-MySQL errors
^
ReferenceError: has is not defined
at Query._callback (/Users/studentlevel/My GitHub Repositories/payfast/controllers/payments.js:20:5)
at Query.Sequence.end (/Users/studentlevel/My GitHub Repositories/payfast/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
at Query.ErrorPacket (/Users/studentlevel/My GitHub Repositories/payfast/node_modules/mysql/lib/protocol/sequences/Query.js:88:8)
at Protocol._parsePacket (/Users/studentlevel/My GitHub Repositories/payfast/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Users/studentlevel/My GitHub Repositories/payfast/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/Users/studentlevel/My GitHub Repositories/payfast/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/Users/studentlevel/My GitHub Repositories/payfast/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:250:12)
[nodemon] app crashed - waiting for file changes before starting...
Se eu deixo o payment.json com currency igual a "BRL", tudo funciona certinho e salva no banco.
Alguma ideia do que pode ser?