1
0
Fork 0
mirror of https://github.com/Luzifer/share.git synced 2024-12-20 18:41:17 +00:00

Implement errors

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-12-02 19:11:09 +01:00
parent 200dcb187d
commit 0f0398aae1
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 62 additions and 3 deletions

View file

@ -1,5 +1,9 @@
fileURL = undefined fileURL = undefined
MSG_NOT_FOUND = 'File not found'
MSG_NOT_PERMITTED = 'Not allowed to access file'
MSG_GENERIC_ERR = 'Something went wrong'
$ -> $ ->
$(window).bind 'hashchange', hashLoad $(window).bind 'hashchange', hashLoad
hashLoad() hashLoad()
@ -9,10 +13,14 @@ hashLoad = ->
embedFileInfo(file) embedFileInfo(file)
embedFileInfo = (file) -> embedFileInfo = (file) ->
if file == ''
return handleErrorMessage MSG_NOT_FOUND
fileURL = file fileURL = file
$.ajax file, $.ajax file,
method: 'HEAD' method: 'HEAD'
success: handleEmbed success: handleEmbed
error: handleError
handleEmbed = (data, status, xhr) -> handleEmbed = (data, status, xhr) ->
type = xhr.getResponseHeader 'Content-Type' type = xhr.getResponseHeader 'Content-Type'
@ -33,3 +41,16 @@ handleEmbed = (data, status, xhr) ->
return return
$('.show-generic').show() $('.show-generic').show()
handleError = (xhr, status) ->
message = switch xhr.status
when 404 then MSG_NOT_FOUND
when 403 then MSG_NOT_PERMITTED
else MSG_GENERIC_ERR
handleErrorMessage message
handleErrorMessage = (message) ->
$('.error').text message
$('.show-loading').hide()
$('.show-error').show()

View file

@ -1,9 +1,15 @@
// Generated by CoffeeScript 1.12.4 // Generated by CoffeeScript 1.12.4
(function() { (function() {
var embedFileInfo, fileURL, handleEmbed, hashLoad; var MSG_GENERIC_ERR, MSG_NOT_FOUND, MSG_NOT_PERMITTED, embedFileInfo, fileURL, handleEmbed, handleError, handleErrorMessage, hashLoad;
fileURL = void 0; fileURL = void 0;
MSG_NOT_FOUND = 'File not found';
MSG_NOT_PERMITTED = 'Not allowed to access file';
MSG_GENERIC_ERR = 'Something went wrong';
$(function() { $(function() {
$(window).bind('hashchange', hashLoad); $(window).bind('hashchange', hashLoad);
return hashLoad(); return hashLoad();
@ -16,10 +22,14 @@
}; };
embedFileInfo = function(file) { embedFileInfo = function(file) {
if (file === '') {
return handleErrorMessage(MSG_NOT_FOUND);
}
fileURL = file; fileURL = file;
return $.ajax(file, { return $.ajax(file, {
method: 'HEAD', method: 'HEAD',
success: handleEmbed success: handleEmbed,
error: handleError
}); });
}; };
@ -42,4 +52,25 @@
return $('.show-generic').show(); return $('.show-generic').show();
}; };
handleError = function(xhr, status) {
var message;
message = (function() {
switch (xhr.status) {
case 404:
return MSG_NOT_FOUND;
case 403:
return MSG_NOT_PERMITTED;
default:
return MSG_GENERIC_ERR;
}
})();
return handleErrorMessage(message);
};
handleErrorMessage = function(message) {
$('.error').text(message);
$('.show-loading').hide();
return $('.show-error').show();
};
}).call(this); }).call(this);

View file

@ -20,7 +20,7 @@
h1 { text-align: center; font-size: 75px; } h1 { text-align: center; font-size: 75px; }
h2 { text-align: center; font-size: 24px; } h2 { text-align: center; font-size: 24px; }
.container { margin-top: 30px; } .container { margin-top: 30px; }
.show-generic, .show-image, .show-video { display: none; } .show-generic, .show-image, .show-video, .show-error { display: none; }
</style> </style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
@ -75,6 +75,13 @@
</div> </div>
</div> </div>
<div class="container show-error">
<h1>
<i class="fa fa-exclamation-circle" aria-hidden="true"></i>
</h1>
<h2 class="error"></h2>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js" <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script> integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>