エラー用のViewに対応
authorOHASHI, Norikazu <katz@neko-mori.sakura.ne.jp>
Fri, 22 Mar 2019 16:23:42 +0000 (01:23 +0900)
committerOHASHI, Norikazu <katz@neko-mori.sakura.ne.jp>
Fri, 22 Mar 2019 16:23:42 +0000 (01:23 +0900)
sinatra/app/controllers/web_gui.rb
sinatra/app/views/error.haml [new file with mode: 0644]
sinatra/app/views/layout.haml
sinatra/app/views/logout.haml
sinatra/app/views/scss/style.scss

index 32a38df..9e09cc9 100644 (file)
@@ -7,10 +7,19 @@ require 'haml'
 require_relative '../models/users_db'
 
 class WebGui < Sinatra::Base
+
+  class WebError < StandardError
+    attr_reader :status
+    def initialize(status)
+      @status = status
+    end
+  end
+  
   set :root, File.join(File.dirname(__FILE__), '..')
   set :views, Proc.new {File.join(root, "views")}
   set :haml, :format => :html5
-
+  set :environment, :production
+  
   use Rack::Session::Cookie,
       :expire_after => 300,
       :secret => 'change'
@@ -42,8 +51,8 @@ class WebGui < Sinatra::Base
       session[:userId] = id;
       redirect "/user_home"
     rescue UserAccount::AlreadyInstanceError 
-      status 406
-      "すでに登録済みのアカウント名が指定されています。"
+      raise WebError.new(406),
+            "すでに登録済みのアカウント名が指定されています。"
     end
   end
 
@@ -61,13 +70,17 @@ class WebGui < Sinatra::Base
       redirect "/user_home"
     rescue UserAccount::NotfondInstanceError,
            UserAccount::AuthenticationError
-      status 401
-      "認証に失敗しました アカウント、 パスワードを確認してください。"
+      raise WebError.new(401),
+            "認証に失敗しました アカウント、 パスワードを確認してください。"
     end
   end
 
   get '/user_home' do
     id = session[:userId]
+    if (id == nil)
+      raize WebError.new(408),
+            "セッション期限切れです。再ログインをしてください。"
+    end
     user = UserAccount.getUser(id)
     @id = id
     @username = user.full_name
@@ -80,5 +93,11 @@ class WebGui < Sinatra::Base
     haml :logout
   end
 
+  error WebError do
+    e = env['sinatra.error']
+    status e.status
+    @error_message = e.message
+    haml :error
+  end
 end
 
diff --git a/sinatra/app/views/error.haml b/sinatra/app/views/error.haml
new file mode 100644 (file)
index 0000000..06a5929
--- /dev/null
@@ -0,0 +1,12 @@
+- # encoding: utf-8
+- error_message =  @error_message
+
+.error
+  %h3
+    エラーが発生しました。
+  %p= error_message
+    
+  .buttoms
+    .buttoms__push
+      %a{ :href => '/'}
+        メインにもどる
index 9156f3c..a05a277 100644 (file)
@@ -31,6 +31,7 @@
             ユーザ情報
           %br
           %a{ :href => "/logout" }
+            ログアウト
         #main= yield
       #foot
         %p
index 086faa3..d99db8f 100644 (file)
@@ -1,5 +1,9 @@
 - # encoding: utf-8
-
 %h2
   おつかれさまでした。
 
+.buttoms
+  .buttoms__push
+    %a{ :href => '/'}
+      メインにもどる
+
index e8be823..8c6ba2d 100644 (file)
@@ -27,7 +27,7 @@ ul {
 
 #sidebar {
     float: left;
-    width: 30%;
+    width: 20%;
     background: #fddea5;
     text-align: left;
     padding-bottom: 3267px;
@@ -36,7 +36,7 @@ ul {
 
 #main {
     float: right;
-    width: 70%;
+    width: 80%;
     background: #f8e58c;
     text-align: center;
     padding-bottom: 3267px;
@@ -55,7 +55,7 @@ ul {
     &__push {
         margin: 20px;
         height: 20px;
-        width: 100px;
+        width: 200px;
         background: #f08300;
         foreground: #3e62ad;
         border: 1px solid #000000;
@@ -67,3 +67,6 @@ ul {
     text-align: left;
 }
 
+.error {
+    background: #cd5e3c
+}