thanks for the amazing job you've done with AOR, this have been a great source of inspiration for me, especially in how to implement a proper redux store.
My question :
If I make a bad request, (code 400), AOR log me out of the app.
How can I prevent this and instead show a notification to my user ?
You can follow the instructions to achieve it:
import { AUTH_ERROR } from 'admin-on-rest';
export default (type, params) => {
if (type === AUTH_ERROR) {
// As I noticed, this code is executed for any error, not only for auth-one.
// So process the error as you wish (show message), don't log out.
}
// Then resolve the promise.
return Promise.resolve();
};
Related
I'm new working with Solidity. I have a very simple contract that is doing some validation
function withdraw(uint amount) external {
uint balance = wallets[msg.sender];
require(balance >= amount, "Pool: not enough balance");
....
and I want to test it so tried sometime like this
try {
await sut.connect(lp1).withdraw(utils.parseUnits("500000000000"))
} catch(e) {
console.log(e)
}
but it's giving me the following error
Uncaught RuntimeError: abort(Error: VM Exception while processing
transaction: reverted with reason string 'Pool: not enough balance').
Build with -s ASSERTIONS=1 for more info.
It seems that my try/catch is not working. I tried expect(....).to.throw() but the result is the same.
It looks like something breaks at VM level and there is nothing to do from JS. I'm using Hardhat, Typescript and Chai.
Any idea? I was trying to find a solution but nothing show up... which is weird, testing these kind of situations is pretty common.
Thanks
I found a way to do it, probably because I'm using types/chai-as-promised.
const withdraw = sut.connect(lp1).withdraw(utils.parseUnits("500"))
await expect(withdraw).eventually.to.rejectedWith(Error, "VM Exception while processing transaction: reverted with reason string 'Pool: not enough balance'")
The trick is not waiting inside expect, and then using eventually.to.rejectedWith
Try this instead. First install ethereum-waffle with your favorite package manager (yarn xD).
import { solidity } from "ethereum-waffle";
import { use, expect } from "hardhat";
use(solidity);
describe("...", async () => {
it("...", async () => {
await expect(erc721.tokenURI(0)).to.be.revertedWith("ERC721: the token does not exist");
});
}
`
I'm dipping my toe into the waters of Axios and async/await at the same time, and am trying to understand something about the control flow. Is the following legitimate?
let loading=true;
(async() => {
let response = null;
try {
response = await axios.get('https://whatever.com/api');
} finally {
loading=false;
}
if(response){
//do something with response here
}
})();
That is, can I count on the request to have returned at the point I am accessing the response variable? I appreciate I could guarantee it is by moving it into the 'try' immediately after the axios get, but then I would have to have the loading=false line before it, as well as in 'finally' (or 'catch'). I need to ensure that loading is set to false before any further actions, whether the request succeeds or fails, and I don't want to repeat myself. Maybe there's a better way of doing this?
Edit
Now that you have changed the question, the previous solution will not be working correctly. The issue is that the code inside the IIFE will be executed after everything else is finished, so loading will never be set to false from the perspective of the outside code. (the other code will be executed, and thеn the IIFE. That's because of the event loop). Your best bet is to make the outside code async and await the axios promise.
If you provide the problem details I might be able to help you refactor it.
Previous answer
I need to ensure that loading is set to false before any further actions
Any code after the await is guaranteed to NOT be loading:
(async() => {
let response = await axios.get('https://whatever.com/api');
// the request is finished, the await guarantees that
})();
If you need error handling, you can wrap it in a try/catch:
(async() => {
try {
let response = await axios.get('https://whatever.com/api');
// definitely not loading
}
catch (e) {
// definitely not loading, but an error occurred
}
})();
I know this should be simple, but a little help would be appreciated; we're all a bit new to using industrial strength typescript packages.
We're building an Angular App and using the #azure/msal-angular library, which for most part works OK; following the tutorials and examples and it generally all make sense.
Apart from what to implement when the "msal:acquireTokenFailure" event is broadcast?
In the ngOnInit() method of our AppComponent we have this line
// Subscriptions and redirects are for jwtTokens
this.subscription = this.broadcastService.subscribe("msal:acquireTokenFailure", () => {
// ToDo: What should be implemented here?
});
In a few posts to the GitHub page for the project contributors have suggested something along the lines of
// Subscriptions and redirects are for jwtTokens
this.subscription = this.broadcastService.subscribe("msal:acquireTokenFailure", () => {
this.subscription.unsubscribe();
this.authService.loginRedirect();
});
Which, as far as I can, will redirect to an AzuerAD login screen but will lose the underlying call details that we trying to get a Token for.
What looks more useful (in pseudo-code) would be something like
// Subscriptions and redirects are for jwtTokens
this.subscription = this.broadcastService.subscribe("msal:acquireTokenFailure", () => {
this.subscription.unsubscribe();
if (isIE) {
this.authService.acquireTokenRedirect(userRequest);
} else {
this.authService.acquireTokenPopup(userRequest);
}
});
The question being, is this a valid approach; and where would we recover the userRequest parameters from?
Please, please, don't redirect me to the Microsoft docs; I've spent hours going round in circles following the same links...
I am using admin-on-rest (1.3.2) and trying to skip the default behaviour of calling AUTH_LOGOUT action on Promise rejection. I want to drop my behavior.
I found an issue on their Github Issues:
https://github.com/marmelab/admin-on-rest/issues/894, but without much information about the implementation here.
In the browser console, I see that executed saga is here:
https://github.com/marmelab/admin-on-rest/blob/v1.3.2/src/sideEffect/saga/crudResponse.js#L92-L97
I've just realized why what AUTH_LOGOUT happening.
My AUTH_ERROR check:
if (type === AUTH_ERROR) {
const { status } = params
if (status === 401) {
localStorage.removeItem('admin')
return Promise.reject()
}
}
Does not have check for other status codes rather than 401 and there was no Promise.resolve returned, which leads to Promise.reject('Unknown method') returned.
I have a simple ExtJs (3.4) Grid with a Writer. When the user makes some changes the store is saved to the server as follows:
store.on('save', afterSave(resp));
All is fine. However, I want to get a response as to wheather the record has been saved successfully, failed or an update conflict happed. How to best do this?
Are you using Ext.data.proxy.Ajax to load your stores? If so, you can use the reader property to evaluate and handle the server responses.
Another option would be to make AJAX called directly and handle the responses from there as well
I used exception listener to parse the data as suggested here. But, is this the right way to do this.
Ext.data.DataProxy.addListener('exception', function(proxy, type, action,
options, res) {
if (type == 'response') {
var success = Ext.util.JSON.decode(res.responseText).success;
if (success) {
console.log('UPDATE OK');
} else {
console.log('UPDATE FAILED');
}
}
});