Ace doesn't provide a way to detect the 404 error, but you can set mode to text before setting it to non existent mode, this way if request returns 404 mode will remain text.
Still better way is to use built in list of all available modes https://github.com/ajaxorg/ace/blob/master/lib/ace/ext/modelist.js#L173