• 検索結果がありません。

エラーページのカスタマイズ

第 9 章 アプリケーションのデプロイ 49

10.3 エラーページのカスタマイズ

10.3.1 エラーハンドリングの仕方

webapp2では、エラーのハンドリングが用意されています。次のコードは、404エラーをハンドリングしてい

ます。

1 def handle_404(request, response, exception):

2 logging.exception(exception)

3 response.write('Oops! I could swear this page was here!')

4 response.set_status(404)

5

6 app = webapp2.WSGIApplication([

7 webapp2.Route(r'/', handler='handlers.HomeHandler', name='home')

8 ])

9 app.error_handlers[404] = handle_404

手順

エラーハンドリングは次のような手順で実装します。

1. エラーハンドリングをするための、ハンドラーオブジェクトを作成する 2. WSGIApplicationオブジェクトにエラーメソッドを登録する

手順1. エラーハンドリングをするための、メソッドを作成する

エラー処理をするためのエラーハンドラクラスを作成します。ここでは、404エラーの処理を行うためのメソッド

handle_404を作成しています。resuponnseオブジェクトに、エラーに対応するステータスコードをセットします。

ここでは、404をセットしています。また、ソースの先頭行に

# coding: utf-8

-*-を追加することで、日本語のメッセージを表示することができます。

1 # coding: utf-8

-*-2 # ......

3

4 def handle_404(request, response, exception):

5 response.write('ページが見つかりませんでした。')

6 response.set_status(404)

5810Webページの追加

手順2. WSGIApplicationオブジェクトにエラーメソッドを登録する

WSGIApplicationオブジェクトの、error_handlers配列の、エラーコード番目に対して、ハンドラークラスをセッ トします。

1 app = webapp2.WSGIApplication([

2 webapp2.Route('/', handler='handlers.HomeHandler', name='home')

3 ])

4 app.error_handlers[404] = handle_404

main.py

main.pyは次のような内容になります。

1 # coding: utf-8

-*-2 #!/usr/bin/env python

3 #

4 # Copyright 2007 Google Inc.

5 #

6 # Licensed under the Apache License, Version 2.0 (the "License");

7 # you may not use this file except in compliance with the License.

8 # You may obtain a copy of the License at

9 #

10 # http://www.apache.org/licenses/LICENSE-2.0

11 #

12 # Unless required by applicable law or agreed to in writing, software

13 # distributed under the License is distributed on an "AS IS" BASIS,

14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

15 # See the License for the specific language governing permissions and

16 # limitations under the License.

17 #

18 import webapp2

19 20

21 class MainHandler(webapp2.RequestHandler):

22 def get(self):

23 self.response.write('Hello world!')

24 25

26 class MyPageHandler(webapp2.RequestHandler):

27 def get(self):

28 self.response.write('Hello MyPage!')

29 30

31 def handle_404(request, response, exception):

32 response.write('ページが見つかりませんでした。')

10.3.1 エラーハンドリングの仕方

webapp2では、エラーのハンドリングが用意されています。次のコードは、404エラーをハンドリングしてい

ます。

1 def handle_404(request, response, exception):

2 logging.exception(exception)

3 response.write('Oops! I could swear this page was here!')

4 response.set_status(404)

5

6 app = webapp2.WSGIApplication([

7 webapp2.Route(r'/', handler='handlers.HomeHandler', name='home')

8 ])

9 app.error_handlers[404] = handle_404

手順

エラーハンドリングは次のような手順で実装します。

1. エラーハンドリングをするための、ハンドラーオブジェクトを作成する 2. WSGIApplicationオブジェクトにエラーメソッドを登録する

手順1. エラーハンドリングをするための、メソッドを作成する

エラー処理をするためのエラーハンドラクラスを作成します。ここでは、404エラーの処理を行うためのメソッド

handle_404を作成しています。resuponnseオブジェクトに、エラーに対応するステータスコードをセットします。

ここでは、404をセットしています。また、ソースの先頭行に

# coding: utf-8

-*-を追加することで、日本語のメッセージを表示することができます。

1 # coding: utf-8

-*-2 # ......

3

4 def handle_404(request, response, exception):

5 response.write('ページが見つかりませんでした。')

6 response.set_status(404)

5810Webページの追加

手順2. WSGIApplicationオブジェクトにエラーメソッドを登録する

WSGIApplicationオブジェクトの、error_handlers配列の、エラーコード番目に対して、ハンドラークラスをセッ トします。

1 app = webapp2.WSGIApplication([

2 webapp2.Route('/', handler='handlers.HomeHandler', name='home')

3 ])

4 app.error_handlers[404] = handle_404

main.py

main.pyは次のような内容になります。

1 # coding: utf-8

-*-2 #!/usr/bin/env python

3 #

4 # Copyright 2007 Google Inc.

5 #

6 # Licensed under the Apache License, Version 2.0 (the "License");

7 # you may not use this file except in compliance with the License.

8 # You may obtain a copy of the License at

9 #

10 # http://www.apache.org/licenses/LICENSE-2.0

11 #

12 # Unless required by applicable law or agreed to in writing, software

13 # distributed under the License is distributed on an "AS IS" BASIS,

14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

15 # See the License for the specific language governing permissions and

16 # limitations under the License.

17 #

18 import webapp2

19 20

21 class MainHandler(webapp2.RequestHandler):

22 def get(self):

23 self.response.write('Hello world!')

24 25

26 class MyPageHandler(webapp2.RequestHandler):

27 def get(self):

28 self.response.write('Hello MyPage!')

29 30

31 def handle_404(request, response, exception):

32 response.write('ページが見つかりませんでした。')

10.3. エラーページのカスタマイズ 59

33 response.set_status(404)

34

35 app = webapp2.WSGIApplication([

36 ('/', MainHandler),

37 ('/mypage', MyPageHandler)

38 ], debug=True)

39

40 app.error_handlers[404] = handle_404

確認

存在しないURLにアクセスし、ページが見つかりませんでした。と表示されることを確認します。

10.3.2 実習 2. エラーページのカスタマイズ

HelloWorldアプリケーションに404エラーページを追加します。

10.3.3 確認

存在しないURLにアクセスし、作成したエラーページが表示されることを確認する。

10.3.4 解答

解答ドキュメントを参照してください。

6010Webページの追加

第 11

ログ

11.1 ログの確認

プログラムが正しく動作しない場合、エラーログを確認して問題の手がかりを得て修正します。

App Engineでもエラーログを確認する方法が提供されており、ランチャーの[Logs]ボタンから確認することがで

きます。

[Log]ボタンをクリックすると[LogConsole]が起動します。

33 response.set_status(404)

34

35 app = webapp2.WSGIApplication([

36 ('/', MainHandler),

37 ('/mypage', MyPageHandler)

38 ], debug=True)

39

40 app.error_handlers[404] = handle_404

確認

存在しないURLにアクセスし、ページが見つかりませんでした。と表示されることを確認します。

10.3.2 実習 2. エラーページのカスタマイズ

HelloWorldアプリケーションに404エラーページを追加します。

10.3.3 確認

存在しないURLにアクセスし、作成したエラーページが表示されることを確認する。

10.3.4 解答

解答ドキュメントを参照してください。

6010Webページの追加

第 11

ログ

11.1 ログの確認

プログラムが正しく動作しない場合、エラーログを確認して問題の手がかりを得て修正します。

App Engineでもエラーログを確認する方法が提供されており、ランチャーの[Logs]ボタンから確認することがで

きます。

[Log]ボタンをクリックすると[LogConsole]が起動します。