Skip to content

Commit df4cfcc

Browse files
committed
fix failed github api request
1 parent d643eb3 commit df4cfcc

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

js/index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,23 @@ $(document).ready(function() {
4040

4141
async function fetchCommits() {
4242
const response = await fetch(`https://api.github.com/repos/${github_username}/${github_repository}/commits`);
43-
const commits = await response.json();
4443

45-
const commitDate = formatDate(commits[0].commit.author.date);
44+
let displayText
4645

47-
// you could also display commit message
48-
const commitMessage = commits[0].commit.message;
49-
const cuttoff = 100;
50-
const commitMessageDisplay = commitMessage.length > cuttoff ? commitMessage.substring(0, cuttoff) + "..." : commitMessage;
51-
52-
const displayText = `Last updated: ${commitDate}`;
46+
if (response.ok) {
47+
const commits = await response.json();
48+
49+
const commitDate = formatDate(commits[0].commit.author.date);
50+
51+
// you could also display commit message
52+
const commitMessage = commits[0].commit.message;
53+
const cuttoff = 100;
54+
const commitMessageDisplay = commitMessage.length > cuttoff ? commitMessage.substring(0, cuttoff) + "..." : commitMessage;
55+
56+
displayText = `Last updated: ${commitDate}`;
57+
} else {
58+
displayText = `Last updated: recently`;
59+
}
5360

5461
document.getElementById('updateMessage').innerHTML = displayText
5562
}

0 commit comments

Comments
 (0)