Handle errors in embed
authorChocobozzz <me@florianbigard.com>
Thu, 19 Apr 2018 16:06:59 +0000 (18:06 +0200)
committerChocobozzz <me@florianbigard.com>
Thu, 19 Apr 2018 16:06:59 +0000 (18:06 +0200)
client/src/standalone/videos/embed.html
client/src/standalone/videos/embed.scss
client/src/standalone/videos/embed.ts

index a359255dacf32227d509c8915f0ab2fae395e404..b60b03a2279327d3884450fde9f4c8dc5a350fc3 100644 (file)
 
   <body>
 
+    <div id="error-block">
+      <h1 id="error-title">Sorry</h1>
+
+      <div id="error-content"></div>
+    </div>
+
     <video id="video-container" class="video-js vjs-peertube-skin">
     </video>
 
index fc7135d643877f8d211f1a70cd2cf06c1b8b6eb0..37c1df6c4730f66464bd7fe9a5ee0142c77f00ad 100644 (file)
@@ -4,6 +4,16 @@
 @import '~videojs-dock/dist/videojs-dock.css';
 @import '../../sass/video-js-custom.scss';
 
+[hidden] {
+  display: none !important;
+}
+
+body {
+  font-family: $main-fonts;
+  font-weight: $font-regular;
+  color: #000;
+}
+
 video {
   width: 99%;
 }
@@ -43,3 +53,38 @@ html, body {
     }
   }
 }
+
+#error-block {
+  display: none;
+
+  flex-direction: column;
+  align-content: center;
+  justify-content: center;
+  text-align: center;
+  background-color: #141313;
+  width: 100%;
+  height: 100%;
+  color: white;
+  box-sizing: border-box;
+  font-family: sans-serif;
+
+  #error-title {
+    font-size: 45px;
+    margin-bottom: 5px;
+  }
+
+  #error-content {
+    font-size: 24px;
+  }
+}
+
+@media screen and (max-width: 300px) {
+  #error-block {
+    font-size: 36px;
+
+    #error-content {
+      font-size: 14px;
+    }
+  }
+}
+
index a99bc586fd71de6dd4251684d05e31f82822b46d..aa418d2d49e92ce21c41d1c9f11f889a7befa124 100644 (file)
@@ -9,19 +9,53 @@ function getVideoUrl (id: string) {
   return window.location.origin + '/api/v1/videos/' + id
 }
 
-async function loadVideoInfo (videoId: string): Promise<VideoDetails> {
-  const response = await fetch(getVideoUrl(videoId))
-  return response.json()
+function loadVideoInfo (videoId: string): Promise<Response> {
+  return fetch(getVideoUrl(videoId))
+}
+
+function removeElement (element: HTMLElement) {
+  element.parentElement.removeChild(element)
+}
+
+function displayError (videoElement: HTMLVideoElement, text: string) {
+  // Remove video element
+  removeElement(videoElement)
+
+  document.title = 'Sorry - ' + text
+
+  const errorBlock = document.getElementById('error-block')
+  errorBlock.style.display = 'flex'
+
+  const errorText = document.getElementById('error-content')
+  errorText.innerHTML = text
+}
+
+function videoNotFound (videoElement: HTMLVideoElement) {
+  const text = 'This video does not exist.'
+  displayError(videoElement, text)
+}
+
+function videoFetchError (videoElement: HTMLVideoElement) {
+  const text = 'We cannot fetch the video. Please try again later.'
+  displayError(videoElement, text)
 }
 
 const urlParts = window.location.href.split('/')
 const videoId = urlParts[urlParts.length - 1]
 
 loadVideoInfo(videoId)
-  .then(videoInfo => {
+  .then(async response => {
     const videoContainerId = 'video-container'
-
     const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
+
+    if (!response.ok) {
+      if (response.status === 404) return videoNotFound(videoElement)
+
+      return videoFetchError(videoElement)
+    }
+
+    const videoInfo: VideoDetails = await response.json()
+
     let autoplay = false
     let startTime = 0