Processing an exception through Apollo Server (NestJS) - node.js

is there a way how to run an exception through the apollo exception handler manually?
I have 90% of the application in GraphQL but still have two modules as REST and I'd like to unify the way the exceptions are handled.
So the GQL queries throw the standard 200 with errors array containing message, extensions etc.
{
"errors": [
{
"message": { "statusCode": 401, "error": "Unauthorized" },
"locations": [{ "line": 2, "column": 3 }],
"path": [ "users" ],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"response": { "statusCode": 401, "error": "Unauthorized" },
"status": 401,
"message": { "statusCode": 401, "error": "Unauthorized" }
}
}
}
],
"data": null
}
where the REST throws the real 401 with JSON:
{
"statusCode": 401,
"error": "Unauthorized"
}
So can I simply catch and wrap the exception in the Apollo Server format or do I have to format my REST errors manually? Thanks
I am using NestJS and the GraphQL module.

You can set up a custom exception filter which catches the REST-Api errors and wraps them in the Apollo Server format. Something like:
#Catch(RestApiError)
export class RestApiErrorFilter implements ExceptionFilter {
catch(exception: RestApiError, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const status = 200;
response
.status(status)
.json(RestApiErrorFilter.getApolloServerFormatError(exception);
}
private static getApolloServerFormatError(exception: RestApiErrorFilter) {
return {}; // do your conversion here
}

Related

Bad Request for an omitted field in GQL

I'm getting "Bad request" response with message "roleId must be a string" when running a mutation. I don't know why since the "roleId" field is optional.
Schema
input CustomerUpdateInput {
name: String
roleId: String
}
updateCustomer(customerId: String!, customer: CustomerUpdateInput!): Customer!
Mutation (ERROR)
mutation updateCustomer{
updateCustomer(customerId:"62c6d6ba303c734ef44ea4ed",
customer: {name:"Pablo"}
),
{id, name }
}
Mutation (WITHOUT ERROR)
mutation updateCustomer{
updateCustomer(customerId:"62c6d6ba303c734ef44ea4ed",
customer: {
name:"Pablo",
roleId:"62c6d64f303c734ef44ea4d8"
}
),
{id, name }
}
Error
{
"errors": [
{
"message": "Bad Request Exception",
"extensions": {
"code": "BAD_USER_INPUT",
"response": {
"statusCode": 400,
"message": [
"roleId must be a string"
],
"error": "Bad Request"
}
}
}
],
"data": null
}

How to send multipart/form data from postman

When I am sending request from postman>body>row and content-type is application/JSON then my request is the successful but same way I am trying to send from multipart/form-data it throws 400 error.
I have changes the header part also in the header section of postman but even though it is giving the same error.
exports['v1'] = (request, response) => {
/* Convert payload to Modal */
console.log(request.body);
const media = new MediaModel(request.body);
/* Validate Payload */
const error = media.validateSync();
if (error) {
return ResUtil.invalidInput(response, error.errors, 'Invalid Payload');
}
/* Process Request */
MediaModel.collection.insertOne(media, (error, data) => {
if (error) {
return ResUtil.error(response, error, 'Error saving media');
}
return ResUtil.created(response, data);
});
};
I am getting below as output as success 200 response
{
"_id": "5d3d5e33b0e15a1adc718a1e",
"setId": "5d384fdbc999c40b08ec4ed2"
}
and when I am applying form-data then
{
"error": {
"setId": {
"message": "Field is required",
"name": "ValidatorError",
"properties": {
"message": "Field is required",
"type": "required",
"path": "setId"
},
"kind": "required",
"path": "setId"
}
},
"message": "Invalid Payload"
}

Amazon Alexa SetVolume Directive

I'm trying to implement the Alexa.Speaker interface with the Node.js v2 sdk.
When testing, I always get the output: There was a problem with the requested skill's response and this error in my CloudWatch logs:
{
"type": "SessionEndedRequest",
"requestId": "amzn1.echo-api.request.bf854d70-70c1-4e7f-b2b0-76c4574244a5",
"timestamp": "2018-10-22T16:58:12Z",
"locale": "en-US",
"reason": "ERROR",
"error": {
"type": "INVALID_RESPONSE",
"message": "An exception occurred while dispatching the request to the skill."
}
}
Here's my code.
return handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Speaker',
name: 'SetVolume',
payload: {
volume: volume,
},
token: 'correlationToken',
})
.getResponse();
I've implemented the Connections.SendRequest interface in this way before, so I'm not sure what's wrong/different with Alexa.Speaker.

Error when I 'm trying to create message draft using Microsoft Graph API

I used this instructions: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_post_messages
I got bearer token and users, but when I try create message using Postman it throw exception
My request:
https://graph.microsoft.com/v1.0/users/4850bf92-08ff-41f3-9891-51561239aaa54/messages
{
"subject":"Did you see last night's game?",
"importance":"Low",
"body":{
"contentType":"HTML",
"content":"They were <b>awesome</b>!"
},
"toRecipients":[
{
"emailAddress":{
"address":"user#mail.com"
}
}
]
}
Response:
{
"error": {
"code": "ResourceNotFound",
"message": "Resource could not be discovered.",
"innerError": {
"request-id": "2b956003-3e1a-4d0d-b5dd-e7f17625e1ac",
"date": "2018-10-04T17:13:57"
}
}
}

Getting 403 Forbidden while posting message to google+ using nodejs

I'm actually implementing Social sharing in my project, so i am doing google+ sharing. While posting message to Google+ using nodejs,I'm getting 403 forbidden error do i need to configure anything on google+ account so that posted message is seen at google+
var params = { "object": {
"originalContent": "hello"
},
"access": {
"items": [
{
"type": "mycircle"
}
],
"domainRestricted": true
}
};
var headers = {
Authorization: 'Bearer ' + google_access_token
};
request.post(shareApiUrl, {
url: 'https://www.googleapis.com/plusDomains/v1/people/{{userid}}/activities',
headers: headers,
body: params,
json: true
}, function (err, response, body) {
console.log(body)
)}
Error Description:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
}
For Posting a feed/or message we should have Google Apps account, but it is impossible with regular GMail account, or your Apps admin hasn't enabled Google+ for the Apps domain.

Resources