How can I change the status code of App sync AWS? - javascript

That's the error data returned from App Sync AWS:
{
"data": {
"getContentById": null
},
"errors": [
{
"path": [
"getContentById"
],
"data": null,
"errorType": "Lambda:Handled",
"errorInfo": null,
"locations": [
{
"line": 1,
"column": 2,
"sourceName": null
}
],
"message": "ID is not found"
}
]
}
How can I change the 200 Status code from my lambda function?
Screen shot from PostMan

Currently you cannot customize the error status code in AWS AppSync. The suggested approach is to use errorType in the error response. You can use $util.appendError or $util.error methods in your velocity mapping template to define the error type.

Related

SequelizeDatabaseError: Sending the error in the response

Logging an error and sending it back in the response gives entirely different results even though it is the same variable!
adminservices.js
var updateAdminById = function(new_admin_data, callback) {
logger.info("new_admin_data: " + JSON.stringify(new_admin_data));
logger.info("new_admin_data.id: " + new_admin_data.id);
models.admin
// .update({ admin_password: passwordCypher }, { where: { id: admin_id } })
.update(new_admin_data, { where: { id:new_admin_data.id } })
.then(function(admin) {
callback({
Update: true,
admin
})
})
.catch(function(error) {
callback({
Update: false,
error
})
});
};
admin_routes.js
router.post("/updateAdminAccountById", function(req, res) {
adminService.updateAdminById(req.body, (updateData) => {
if (updateData.update) {
//Update is succesfull return updated admin data
res.status(200).json(updateData.admin)
} else {
//Update failed return error
logger.error(updateData.error);
res.status(400).json(updateData.error)
}
})
});
In the terminal, logging the error gives this:
2020-02-06T12:02:24+0100 admin_routes.js:459
SequelizeDatabaseError: invalid input value for enum enum_admins_role:
"Administrateur"
But, the response in POSTMAN is this:
{
"name": "SequelizeDatabaseError",
"parent": {
"name": "error",
"length": 179,
"severity": "ERROR",
"code": "22P02",
"file": "d:\\pginstaller_12.auto\\postgres.windows-x64\\src\\backend\\utils\\adt\\enum.c",
"line": "133",
"routine": "enum_in",
"sql": "UPDATE \"admins\" SET \"nom\"=$1,\"email\"=$2,\"mot_de_passe\"=$3,\"role\"=$4,\"id\"=$5,\"updatedAt\"=$6 WHERE \"id\" = $7",
"parameters": [
"NewUpdatedValue",
"NewEmail",
"NewPassword",
"Administrateur",
"29",
"2020-02-06 11:02:23.391 +00:00",
"29"
]
},
"original": {
"name": "error",
"length": 179,
"severity": "ERROR",
"code": "22P02",
"file": "d:\\pginstaller_12.auto\\postgres.windows-x64\\src\\backend\\utils\\adt\\enum.c",
"line": "133",
"routine": "enum_in",
"sql": "UPDATE \"admins\" SET \"nom\"=$1,\"email\"=$2,\"mot_de_passe\"=$3,\"role\"=$4,\"id\"=$5,\"updatedAt\"=$6 WHERE \"id\" = $7",
"parameters": [
"NewUpdatedValue",
"NewEmail",
"NewPassword",
"Administrateur",
"29",
"2020-02-06 11:02:23.391 +00:00",
"29"
]
},
"sql": "UPDATE \"admins\" SET \"nom\"=$1,\"email\"=$2,\"mot_de_passe\"=$3,\"role\"=$4,\"id\"=$5,\"updatedAt\"=$6 WHERE \"id\" = $7",
"parameters": [
"NewUpdatedValue",
"NewEmail",
"NewPassword",
"Administrateur",
"29",
"2020-02-06 11:02:23.391 +00:00",
"29"
]
}
My question is not why I am having this error. I have succesfully fixed it. My questions are:
How come the error is displayed differently in the terminal and in POSTMAN if it is the same variable?
Why doesn't the error description appear in POSTMAN like it appears in the terminal?
SequelizeDatabaseError is descendant of BaseError which in turn descendant of Error.
Error redefines .toString() to print instance's name and message properties like '${name}: ${message}' if both available.
For more details see:
https://sequelize.org/master/class/lib/errors/base-error.js~BaseError.html
https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Error/toString

Setting up a webhook without Google Cloud Project

Want to set up the webhook on my pc and not on google cloud project like the guide is sugesting, and I'm not quite sure how to.
This is the guide: https://dialogflow.com/docs/getting-started/basic-fulfillment-conversation
It says to create a index.js with the following code in it
/*
* HTTP Cloud Function.
*
* #param {Object} req Cloud Function request context.
* #param {Object} res Cloud Function response context.
*/
exports.helloHttp = function helloHttp (req, res) {
response = "This is a sample response from your webhook!"
//Default response from the webhook to show it's working
res.setHeader('Content-Type', 'application/json');
//Requires application/json MIME type
res.send(JSON.stringify({ "speech": response, "displayText": response
//"speech" is the spoken version of the response, "displayText" is the visual version
}));
};
Never seen exports. before. The guide wants me to do something with 'google functions' and make one called hellohttp or something. Trying to figure out how to do it without google cloud projects.
I installed Node.js and created that file. I set the webhook url to https://my_ip/index.js but doesn't work. Not really sure what to do
this is the JSON I get
{
"id": "9c244834-3ee9-4291-98fd-aab8e975fb1f",
"timestamp": "2018-02-22T23:11:34.067Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "weather TOMORROW in VIRGINIA",
"action": "",
"actionIncomplete": false,
"parameters": {
"date": "2018-02-24",
"geo-city": "Virginia"
},
"contexts": [],
"metadata": {
"intentId": "44486050-dfd5-4c35-bcbf-4e168379df28",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 5006,
"intentName": "Weather"
},
"fulfillment": {
"speech": "I don't know about the weather for 2018-02-24 in Virginia. Sorry!",
"messages": [
{
"type": 0,
"speech": "I don't know about the weather for 2018-02-24 in Virginia. Sorry!"
}
]
},
"score": 1
},
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: Webhook response was empty.",
"webhookTimedOut": false
},
"sessionId": "9cdbd5bc-403e-4840-8970-e420f27d23e2"
}

Loopback: How can we modify the return data of a built in model function

I created a model named member inherited from the built in model User,
But how can I change the format of the login return. ie,
currently the invalid login is returning like
{
"error": {
"statusCode": 401,
"name": "Error",
"message": "login failed",
"code": "LOGIN_FAILED",
"stack": "Error: login failed\n ..."
}
}
I want to change like,
{
"code": 401,
"name": "Error",
"message": "login failed",
"code": "LOGIN_FAILED",
"stack": "Error: login failed\n ..."
}
Also, the success case as well,
ie current return is
{
"id": "P1jAavmCRbiYB1gYaE2snj3I6BayIYOCJ7HsTLeF1bezlEGVAUzjwdxKB5QFH9Vu",
"ttl": 1209600,
"created": "2017-02-02T13:19:05.709Z",
"userId": "5885b186db6df92d3ada7777"
}
I want to change like,
{
"code": 200,
"name": "Success",
"token": "P1jAavmCRbiYB1gYaE2snj3I6BayIYOCJ7HsTLeF1bezlEGVAUzjwdxKB5QFH9Vu",
"ttl": 1209600,
"created": "2017-02-02T13:19:05.709Z",
"userID": "5885b186db6df92d3ada7777"
}
Is it possible or not?
You need to remove strong-error-handler from middlewares and add your custom error handler like this
In config.json you need to do :
...
"remoting" {
...
"handleErrors": false
...
}
...
Also create config.local.js in root/sever folder and add below :
'use strict';
var errorConverter = require('./middlewares/error-converter');
module.exports = {
remoting: {
errorHandler: {
handler: errorConverter()
}
}
};
error-converter.js in middleare folder (or any other place) is the custom error hadnling middleare

$http service throw exception when downloading json file from server

When trying to download a file from server i use $http service as following:
$http({url: url, method: "GET", withCredentials: true});
for some unkown reason only json files causing an exception when getting from server:
SyntaxError: Unexpected token ] in JSON at position 1362
at Object.parse (native)
at tc (https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:14:245)
at bc (https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:77:7)
at https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:77:369
at q (https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:7:322)
at dd (https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:77:351)
at c (https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:78:495)
at https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:112:182
at m.$eval (https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:126:250)
at m.$digest (https://sharon-staging.comilion.com/app/bower_components/angular/angular.min.js:123:365)
The json file is valid, i attached him in the question.
if it's a text file, or even the same file that i added a comment above the json, there is no exception.
If anyone have an idea why do i get this error, it will really really help :)
Thanks
not working file:
{
"users": [
{
"email": "D_user1#e2e.com",
"nickName": "D_user1e2e"
}
],
"groups": [
{
"name": "D_BETWEEN_INSTANCES",
"description": "GROUP OF USERS FROM ALL INSTANCES",
"members": [
"user6#e2e.com",
"A_user6#e2e.com",
"B_user6#e2e.com",
"D_user1#e2e.com"
]
},
]
}
working file:
tests
{
"users": [
{
"email": "D_user1#e2e.com",
"nickName": "D_user1e2e"
}
],
"groups": [
{
"name": "D_BETWEEN_INSTANCES",
"description": "GROUP OF USERS FROM ALL INSTANCES",
"members": [
"user6#e2e.com",
"A_user6#e2e.com",
"B_user6#e2e.com",
"D_user1#e2e.com"
]
},
]
}
Remove the , sign from here-
"D_user1#e2e.com"
]
},
like this-
"D_user1#e2e.com"
]
}

Google Cloud Save with JavaScript/REST getting 409 Conflict

So I'm trying to implement the google appstate cloud save feature to save a string (https://developers.google.com/games/services/web/api/states) but I am getting a 409 conflict error. I must be using it incorrectly. Does anyone have any examples of a JavaScript implementation?
This is how I am going about it:
GET https://www.googleapis.com/appstate/v1/states
returns:
{
"kind": "appstate#listResponse",
"maximumKeyCount": 4,
"items": [
{
"kind": "appstate#getResponse",
"stateKey": 0,
"currentStateVersion": "CNDVopDDwLgC"
}
]
}
then I
PUT https://www.googleapis.com/appstate/v1/states/0
with body:
{
currentVersion: "CNDVopDDwLgC"
data: "some data"
kind: "appstate#updateRequest"
}
which gives the error: 409 Conflict
{
"error": {
"errors": [
{
"domain": "global",
"reason": "OutOfDateVersion",
"message": "The resource version you are attempting to modify does not match the current version on the server."
}
],
"code": 409,
"message": "The resource version you are attempting to modify does not match the current version on the server."
}
}
Does anyone know what I am doing wrong?

Categories

Resources