Skip to content

Commit fcd8ca2

Browse files
committed
show alert box when occur error
1 parent 3e828be commit fcd8ca2

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ <h2>Gist HTML Preview</h2>
3333
</div>
3434
<button id="submit" class="btn btn-primary">Submit</button>
3535
</div>
36+
<br/>
37+
<div id="alert-box"></div>
3638
</div>
3739

3840
<script src="https://cdn.rawgit.com/github/fetch/master/fetch.js"></script>

main.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
(function () {
2+
function showError (message) {
3+
document.getElementById('alert-box').innerHTML
4+
+= '<div class="alert alert-danger">'
5+
+ '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>'
6+
+ message
7+
+ '</div>';
8+
}
9+
210
document.getElementById('submit').onclick = function () {
311
location.search = document.getElementById('gist_id').value + '/'
412
+ document.getElementById('file_name').value;
@@ -20,35 +28,35 @@
2028
document.getElementById('file_name').value = fileName;
2129

2230
// 4-1. check gist id
23-
if (/^[0-9a-f]*$/g.test(gistId) === false) {
24-
console.error('Gist Id ' + gistId + ' is invalid')
31+
if (gistId.length === 0 || /^[0-9a-f]*$/g.test(gistId) === false) {
32+
showError('Gist Id <strong>' + gistId + '</strong> is invalid')
2533
return;
2634
}
2735

2836
// 4-2. check file name
2937
if (typeof fileName !== 'string' || fileName.length === 0) {
30-
console.error('File Name ' + fileName + ' is invalid');
38+
showError('File Name <strong>' + fileName + '</strong> is invalid');
3139
return;
3240
}
3341

3442
// 5. fetch data
3543
fetch('https://api.github.com/gists/' + gistId)
3644
.then(function (res) {
3745
if (res.status !== 200) {
38-
throw new Error('Gist Id ' + gistId + ' is not exist');
46+
throw new Error('Gist Id <strong>' + gistId + '</strong> is not exist');
3947
}
4048
return res.json();
4149
})
4250
.then(function (info) {
4351
if (info.files.hasOwnProperty(fileName) === false) {
44-
throw new Error('File ' + fileName + ' is not exist');
52+
throw new Error('File <strong>' + fileName + '</strong> is not exist');
4553
}
4654

4755
// 6. write data
4856
var content = info.files[fileName].content;
4957
document.write(content);
5058
})
5159
.catch(function (err) {
52-
console.error(err);
60+
showError(err.message);
5361
});
5462
})();

0 commit comments

Comments
 (0)