By looking at this question, I failed to find the answer to my current problem:
I have a custom exception (designed according to this guide):
let StrictOverwrite_destination_error = function(message) {
var error = new Error(message);
error.name = "utils_misc.StrictOverwrite_destination_error";
throw error;
};
which I throw from the function strictOverwrite:
function strictOverwrite ( destination, source, debug ) {
// lots of code snipped
else if ( !destination.hasOwnProperty( property ) && source.hasOwnProperty( property ) )
{
throw new StrictOverwrite_destination_error(
"source.property exists, but destination.property doesn't" );
}
// more code snipped
return destination;
}
Both the error and the function is defined in my module utils_misc, although my custom exception definition isn't exported from the namespace.
This is how I test exception throwing from my strictOverwrite function:
assert.throws(
function() {
utils_misc.strictOverwrite(c, c_dominator, false);
},
function(error) {
return error.name === "utils_misc.StrictOverwrite_destination_error";
}
);
but, alas, Qunit isn't satisfied:
Expected:
function( a ){
[code]
}
Result:
{
"message": "property is not defined",
"name": "ReferenceError"
}
Diff:
function( a ){
[code]{
"message": "property is not defined",
"name": "ReferenceError"
}
Apart from the whole thing not working, one thing that puzzles me is how Qunit acquired this message: "property is not defined". That's not exactly the message produced by my custom StrictOverwrite_destination_error exception.
The test code written in this question works just right. The issue was in the application code being tested.
Related
I am using Vue.js with Vue-Apollo and trying to fetch shared member list using query. I am using the graphQL service in backend.
I am using apollo 'error' function to handle GraphQL error. When the request is made with invalid input, I can see the errors in the network tab, I can see the JSON for the custom errors messages. But I can't console the errors in 'error' function.
Here is the apollo query that is used to fetch shared member list -
apollo: {
sharedMembers: {
query: gql`
query item($uuid: ID) {
item(uuid: $uuid) {
...itemTemplate
members {
...member
permission
}
}
}
${ITEM_TEMPLATE}
${MEMBER}
`,
variables() {
return {
uuid: this.$route.params.uuid,
}
},
update(data) {
return data.item.members
},
error(error) {
console.log('errors', error)
}
},
},
The network response I got -
network_error
Using graphQLErrors
You could get the errors by looking in the error object for graphQLErrors:
error(error) {
console.log('errors', error.graphQLErrors)
}
or
error({ graphQlErrors }) {
console.log('errors', graphQLErrors)
}
Using apollo-error-link
You can use apollo-error-link to help solve your problem if the above doesn't work, docs here.
Here's an example from the docs and I added to it in the networkErrors section to show what you can do to edit the error message you see in your error block, or catch block if its a mutation.
import { onError } from "apollo-link-error";
const link = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
if (networkError) {
// Add something like this to set the error message to the one from the server response
networkError.message = networkError.result.errors[0].debugMessage
console.log(`[Network error]: ${networkError}`)
};
});
And then in your code:
error(error) {
console.log('error-message', error.message)
}
The console should then log your debugMessage from the server.
unfortunately i couldn't find out how i'd handle errors in such of graphql method call, but as an option you could provide onError method to ApolloClient constructor options. first argument is the error object. hopefully it may help. like so..
const apolloClient = new ApolloClient({
uri: 'http://localhost:4000',
onError(err) {
console.log(err)
},
})
I am saving the outlook context and in the saveAsync method i am getting event Id. Sometimes event id contains '/' in it. if i use that eventId with '/' it is failing. can someone help me on this?
Office.context.mailbox.subject.setAsync
(
"subject",
function (asyncResult0)
{
if (asyncResult0.status === Office.AsyncResultStatus.Succeeded)
{
Office.context.mailbox.body.setAsync
(
"sample body",
function (asyncResult1)
{
if (asyncResult1.status === Office.AsyncResultStatus.Succeeded)
{
Office.context.mailbox.item.saveAsync
(
function (result)
{
**let eventId = result.value;**
// Process the result
}
);
}
}
);
}
}
);```
Update your code to:
Office.context.mailbox.subject.setAsync -> Office.context.mailbox.item.subject.setAsync
Office.context.mailbox.body.setAsync -> Office.context.mailbox.item.body.setAsync
If the problem persists, could you please share the screenshot of the result and the scenario you're trying to achieve.
I'm new to aws amplify. I have setup an app using amplify and I have an API that is returning records using GraphQl. I have created a subscription that is supposed to be triggered when creating a new blog entry. The entries are being created. In the documentation https://aws-amplify.github.io/docs/js/api the code samples suggest that I can use the following to subscribe to a mutation. I keep getting an error stating that error TS2339: Property 'subscribe' does not exist on type '{}'. It's coming from the assignment of client. I'm not sure why its saying this and I was hoping that you would be able to help me with this error.
import { onCreateBlog } from './graphql/subscriptions';
//GraphQl subscription
export const onCreateBlog = `subscription OnCreateBlog {
onCreateBlog {
id
name
posts {
items {
id
title
}
nextToken
}
}
}
`;
//ngInit function with an async method
ngOnInit() {
(async () => {
let client = Amplify.configure(awsmobile); // error from here
let subscription = client.subscribe(graphqlOperation(subscriptions.onCreateBlog)).subscribe({
next: data => {
console.log(data);
},
error: error => {
console.warn(error);
}
});
})();
}
I am trying to do customize messaging with GraphQLError.
There are few use cases that I want to handle with GraphQL Error:
when username and password did not match, I want to return customize the message that username and password did not match.
When the user entered an invalid email, I want to return customize the message that entered email is not valid.
And few other use cases.
I created a ValidateError.js File to use GraphQLError handling function:
const { GraphQLError } = require('graphql');
module.exports = class ValidationError extends GraphQLError {
constructor(errors) {
super('The request is invalid');
var err = errors.reduce((result, error) => {
if (Object.prototype.hasOwnProperty.call(result, error.key)) {
result[error.key].push(error.message);
} else {
result[error.key] = [error.message];
}
return result;
}, {});
}
}
Here is the code of my application index file app.js:
app.use('/graphql', graphqlExpress(req => ({
schema,
context: {
user: req.user
},
formatError(err) {
return {
message: err.message,
code: err.originalError && err.originalError.code,
locations: err.locations,
path: err.path
};
}
})));
My question is how can I use this function for grabbing graphQLError
formatError
Thanks in advance.
"apollo-server-express": "^1.3.5"
"graphql": "^0.13.2"
Just throw your error in resolver, formatError function will catch each error thrown in resolver.
Here is my work:
appError.js
class AppError extends Error {
constructor(opts) {
super(opts.msg);
this.code = opts.code;
}
}
exports.AppError = AppError;
throw an custom error in resolver:
throw new AppError({ msg: 'authorization failed', code: 1001 });
catch this error in formatError:
formatError: error => {
const { code, message } = error.originalError;
return { code, message };
},
Other sample:
throw your error in resolver:
const resolvers = {
Query: {
books: () => {
throw new GraphQLError('something bad happened');
}
}
};
catch error in formatError:
graphqlExpress(req => {
return {
schema,
formatError: err => {
console.log('format error');
return err;
}
};
})
Here is the output:
format error
GraphQLError: something bad happened
at books (/Users/ldu020/workspace/apollo-server-express-starter/src/graphql-error/index.js:23:13)
at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql-tools/dist/schemaGenerator.js:518:26
at resolveFieldValueOrError (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:531:18)
at resolveField (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:495:16)
at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:364:18
at Array.reduce (<anonymous>)
at executeFields (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:361:42)
at executeOperation (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:289:122)
at executeImpl (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:154:14)
at Object.execute (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:131:229)
Inside of GraphQLError you should have access to GraphQLErrorExtensions which should allow you to append your custom messages when throwing errors.
This answer was written with knowledge of apollo server throwing custom errors through this extensions option. This may be achievable but more complex. I suggest checking out apollo server error: https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-errors/src/index.ts
It looks like what you can do is just pass any additional information through the extensions, but you will only be able to set it inside the constructor: new GraphQLError('your error message', null, null, null, null, null, {"message1": [ "custom message1" ], "message2": [ "customer message1", "custom message2" ]})
I'm using Ember 1.2.0 and the latest Ember Data Beta and wonder, how to handle server side errors (from API calls).
This question is quite similar, but it doesn't work.
At first, the becameInvalid method doesn't triggered. I'm using ember-validations (do I have to?)
My API sends an 422 status code and responses like that:
{"errors":{"name":["has already been taken"],"initial":["has already been taken"]}}
model.js
Docket.Customer = DS.Model.extend( Ember.Validations, {
name: DS.attr('string'),
initial: DS.attr('string'),
description: DS.attr('string'),
validations: {
name: {
presence: true
}
},
becameError: function() {
alert('there was an error!');
},
becameInvalid: function(errors) {
alert("Record was invalid because: " + errors);
}
});
controller.js
Docket.OrganizationCustomersController = Ember.ArrayController.extend({
actions: {
save: function () {
var customer = this.store.createRecord('customer');
customer.set('name', this.get('name'));
customer.set('initial', this.get('initial'));
customer.set('description', this.get('description'));
customer.save().then(function() {
console.log('jeah')
}, function() {
console.log('nooo')
});
}
}
});
The becameError method gets fired, but the becameInvalid method doesn't.
The second problem: even if the error is triggered, Ember.js adds the new record to the DOM. How can I prevent this behaviour?
Your errors json is ok, I think you are using the DS.RESTAdapter, and it doesn't implement the becameInvalid based in json with errors.
Just DS.ActiveModelAdapter have implemented in the moment, so I recommend you to change your adapter configuration to:
Docket.ApplicationAdapter = DS.ActiveModelAdapter;
In order to keep DS.RestAdapter, you can override its ajaxError method with the one from ActiveModelAdapter.
As for today the code, slightly adapted because some dependencies are needed, would be :
App.ApplicationAdapter = DS.RESTAdapter.extend({
// ... your own customizations,
ajaxError: function(jqXHR) {
var error = this._super(jqXHR);
if (jqXHR && jqXHR.status === 422) {
var response = Ember.$.parseJSON(jqXHR.responseText),
errors = {};
if (response.errors !== undefined) {
var jsonErrors = response.errors;
Ember.EnumerableUtils.forEach(Ember.keys(jsonErrors), function(key) {
errors[Ember.String.camelize(key)] = jsonErrors[key];
});
}
return new DS.InvalidError(errors);
} else {
return error;
}
}
});
Obviously you have a chance here to adapt to your backend specifics: HTTP code (422 is not a standard one) and format.
Source :
http://discuss.emberjs.com/t/how-to-handle-failure-to-save-on-server/3789