Handling server errors with SimpleForm - redux-form

I'm trying to handle server validation errors once submission of a SimpleForm has been done.
My form is quite easily created:
<SimpleForm onSubmit={submitResults}>
<UserForm />
</SimpleForm>
And I have a simple way to throw a submission error, not calling the server for the moment:
const submitResults = (values) => {
throw new SubmissionError({firstname: "Firstname is wrong", lastname: "Lastname is not good", _error: "Submit Failed"});
};
According to https://marmelab.com/admin-on-rest/CreateEdit.html#validation and Redux Form, this should be fine: https://redux-form.com/6.5.0/examples/submitvalidation/.
However, I don't enter submitResults function, and the submission is made to the server which returns a 401 status which never gets understood.
What's wrong with this approach?

Related

Custom GraphQL Error types for Validation

Is this possible to set custom GraphQL Error types for at least BAD_USER_INPUT code?
On the backend I've got an error type something like this:
throw {
type: 'ValidationError',
code: 422,
success: false,
message: `Something didn't work well`,
}
on Frontend I am still getting the same error type when some fields don't pass schema validation:
That's not really comfortable to take care of the both error formats on the frontend.
Is it possible to somehow:
Except errors and BAD_USER_INPUT receive data response 200 and check it by success?
Or maybe it's just possible to make my format of error (the one I'm sending form backend)?
I already wanted to make it using global error handler:
export default ({ graphQLErrors, networkError, operation, forward }, nuxtContext) => {
console.log('Global error handler')
console.log(graphQLErrors, networkError, operation, forward)
}
apollo: {
...
// setup a global error handler (see below for example)
errorHandler: '~/plugins/graphql/apollo-error-handler.js',
...
}
But it doesn't seem to work at all
Formatting errors can be achieved in Apollo Graphql server using formatError function. You can raise custom errors from your application from which the necessary error response can be constructed at formatError function.

How to mock or spy on recaptcha in Jasmine

I added recaptcha-v2 in my Angular6 forms. How do I unit-test the form? I want to test that the form under test is invalid if recaptcha isn't clicked and also the form gets submitted if recaptcha is clicked.
The test I have written so far is
fit('it should emit form values if both username and password fields in the login form are correctly filled', () => {
const loginComponent = component;
spyOn(loginComponent.formOutputEvent, 'emit');
const formControls = loginComponent.loginForm.controls;
const email = 'test#test.com';
const password = 'testpassword';
formControls['userName'].setValue(email);
formControls['password'].setValue(password);
formControls['recaptcha'].setValue({}); //what value to set here?
loginComponent.loginFormSubmitted();
expect(loginComponent.formOutputEvent.emit).toHaveBeenCalledWith(new LoginFormValues(email, password));
});
I am getting error Expected spy emit to have been called with [ LoginFormValues({ username: 'test#test.com', password: 'testpassword', recaptcha: '' }) ] but actual calls were [ LoginFormValues({ username: 'test#test.com', password: 'testpassword', recaptcha: Object({ }) }) ].
Interesting, the following works even though LoginFormValues expects the 3rd argument to be of type string but {} isn't
expect(loginComponent.formOutputEvent.emit).toHaveBeenCalledWith(new LoginFormValues(email, password,{}));
But this fails because {}.toString(). gets converted to [Object object].
expect(loginComponent.formOutputEvent.emit).toHaveBeenCalledWith(new LoginFormValues(email, password,{}.toString));
I am still waiting for better answer

signin/verifyState is not invoked after notifySuccess

notifySuccess doesn't fire signin/veriyState event.
My TeamsMessagingExtensionQuery handler returns something like this:
...
return {
composeExtension: {
type: "auth",
suggestedActions: {
actions: [
{
type: "openUrl",
value: "https://something.com/login/",
title: "Sign in to this app"
}
]
}
}
} as MessagingExtensionResponse;
...
This response shows the searchCmd extension with a sign-in message correctly. On clicking the sign-in, the prompt shows the login page correctly as well, according to the url that was passed in the response.
However, when in the login page I try to pass some test token data using the notifySuccess api,the composeExtension/query command gets invoked (with the state property), but there is no signin/verifyState event getting invoked.
On the login/client side, I am using the following two lines of code:
MSTeams.initialize();
MSTeams.authentication.notifySuccess('<test_token>');
I haven't tried this signin combo (trying to sign in for a message extension), but it seems to me that Teams won't actually invoke "signin/verifyState" in this case, and that's only for a "vanilla" bot authentication flow. In this case, it seems from the docs like you're meant to complete the authentication inside the "composeExtension/query" instead...

The browser overwrites the content of the message after receiving the response

I have a little problem with DingoAPI and Vue.js when I'm trying to get my error message from response. I think the browser is replacing my custom message by the default one. Here is my code:
PHP script
if($request->readerId){
Return succes (this works properly)
else{
return $this->response->error(
'No reader',
400 //(or diffrent code)
);
}
Vue.js script
await axios.post(API_URL + 'card/', {
some data
}, {
headers: {
headers
},
}).then(({data}) => {
context.commit(SET_RESPONSE, data);
}).catch((error) => {
console.log(error.message);
throw error
})
When I'm trying to look on my message in the network tab I can see (so DingoAPI did it correctly):
{"message":"No reader","status_code":400}
But when I'm using console.log(error.message) or trying to show it on the page there is standard error message:
Request failed with status code 400
Is there a way to set error message with DingoAPI and catch it in my .js script?
Maybe I need to write my own custom exception?
What you want is access to the data of the response from your error variable.
console.log(error.response.data.message); // No reader
Otherwise you can log error.response to see the object:
console.log(error.response);
If you wonder why it's printing Request failed with status code 400:
The problem is when the console.log tries to output the error, the string representation is printed, not the object structure, so you do not see the .response property.
Source: https://github.com/axios/axios/issues/960#issuecomment-309287911

Parse.User.save() doesn't work with Facebook API

I'm using the Facebook SDK to auth a user, and trying to save the user's email to the record after authenticating. However, I keep getting an error on the save call.
The code in question:
Parse.FacebookUtils.logIn({
access_token: authResponse.access_token,
expiration_date: expire_date.toISOString(),
id: response.id
},
{
success: function(user) {
console.log("success!");
user.set({"email":response.email});
user.save();
window.App.navigate("#myplaces", {trigger:true});
},
...
That user.save() call returns error occurred: http://www.parsecdn.com/js/parse-1.1.14.min.js:1: TypeError: 'undefined' is not an object.
According to the docs, I have to be in an authentication call (".logIn", etc.) to perform save(), so I'm wondering if this still works with Parse.FacebookUtils.logIn. Seems like it should.
Ideas as to why this isn't working? The ideal behavior is to log the user in, retrieve information from the FB response, and save that back to the user record on Parse.
Thanks!
Justin
Not sure about this but I had the same problem in Cloud Code and I used success/error callbacks when calling save(...):
user.save(null, {
success: function(){
// Code
},
error: function(){
// Code
}
});
See also here: https://parse.com/questions/saving-a-relation-on-the-current-user

Resources