Because error checking dies on the second try, it successfully tries with a blank stash id, but an invalid folder id. Upload dies because the invalid folder id error is the second error.
Fix would probably be to have an extra variable showing a 'fixed' retry that's not counted against the count, to get an extra retry which would allow it to get past the two errors and should upload successfully.