Exceptable - Exception Propagation for PG
-
Upload
command-prompt-inc -
Category
Documents
-
view
523 -
download
2
description
Transcript of Exceptable - Exception Propagation for PG
![Page 1: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/1.jpg)
Exceptable
Tuesday, October 20, 2009
![Page 2: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/2.jpg)
Exception Propagation, in PostgreSQL and PythonAurynn Shaw, Commandprompt, Inc.
PostgreSQL Conference West, 2009
Tuesday, October 20, 2009
![Page 3: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/3.jpg)
Background
• Large, enterprise Python + Pylons application
• Simpycity ORM utilizes significant stored procedures
• Strong procedural interfaces
Tuesday, October 20, 2009
![Page 4: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/4.jpg)
Stored Procedures are Rare
• Most people don’t use Stored Procedures.
Tuesday, October 20, 2009
![Page 5: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/5.jpg)
The Present SystemWherein
• A Database Query
• A Stored Procedure
• Or, A Database Query
• Exceptions Galore
Tuesday, October 20, 2009
![Page 6: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/6.jpg)
A Database Query
•The App queries the Database
Tuesday, October 20, 2009
![Page 7: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/7.jpg)
A Database Query
• The App queries the Database
• Any variety of query
Tuesday, October 20, 2009
![Page 8: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/8.jpg)
A Database Query
>>> c.Raw("SELECT * FROM auth_only")()
Traceback (most recent call last):
... <SNIP> ...
psycopg2.ProgrammingError: permission denied for relation auth_only
Tuesday, October 20, 2009
![Page 9: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/9.jpg)
A Database Query
• The App queries the Database
• Any variety of query
• Or A Stored Procedure
Tuesday, October 20, 2009
![Page 10: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/10.jpg)
Or a Stored Procedure
CREATE OR REPLACE FUNCTION except_test() RETURNS VOID AS $$
BEGIN
RAISE EXCEPTION 'Test!';
END;
$$ LANGUAGE PLPGSQL;
>>> c.Function("except_test")()
Traceback (most recent call last):
... <SNIP> ...
psycopg2.InternalError: Test!
Tuesday, October 20, 2009
![Page 11: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/11.jpg)
Beget an Exception
• Stored procedures raise InternalErrors
• Every procedural exception becomes this
• Permissions violations raise ProgrammingErrors
• I haven’t tested if this is all PG exceptions.
Tuesday, October 20, 2009
![Page 12: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/12.jpg)
Seriously?
• This is what we have to work with?
Tuesday, October 20, 2009
![Page 13: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/13.jpg)
Why this is ImportantWherein
• Why?
• Defining your APIs
• Separation of Concerns
• Procedural Spaghetti
Tuesday, October 20, 2009
![Page 14: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/14.jpg)
Why?
• Limited information to propagate upwards
• Stored Procedures are Harder to use
• Permissions violations are difficult to detect
Tuesday, October 20, 2009
![Page 15: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/15.jpg)
API Undefined
• Exceptions are part of your API
Tuesday, October 20, 2009
![Page 16: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/16.jpg)
API Undefined
• Exceptions are part of your API
• Easily overlooked
Tuesday, October 20, 2009
![Page 17: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/17.jpg)
API Undefined
• Exceptions are part of your API
• Easily overlooked
• Delineations will make life easier
Tuesday, October 20, 2009
![Page 18: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/18.jpg)
Separation of Concerns
• The Database is for Database Logic
Tuesday, October 20, 2009
![Page 19: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/19.jpg)
Separation of Concerns
• The Database is for Database Logic
• Harder to write Data Logic
Tuesday, October 20, 2009
![Page 20: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/20.jpg)
Spaghetti
• InternalErrors everywhere
Early version of our app didn’t have Exceptable - we were left catching InternalErrors and guessing at what the error was, based on timing.
Tuesday, October 20, 2009
![Page 21: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/21.jpg)
Spaghetti
• InternalErrors everywhere
• Insufficiency of Information
Tuesday, October 20, 2009
![Page 22: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/22.jpg)
Spaghetti
• InternalErrors everywhere
• Insufficiency of Information
• Considerable Repeated Code
Tuesday, October 20, 2009
![Page 23: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/23.jpg)
A Saving Grace
• Violating Procedure Signatures -> Python DataError
Tuesday, October 20, 2009
![Page 24: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/24.jpg)
A Better DealWherein
• Database API
• Easy Python implementation
• Universality
• Exceptable
Tuesday, October 20, 2009
![Page 25: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/25.jpg)
Database API
• Easier to define an API
Tuesday, October 20, 2009
![Page 26: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/26.jpg)
Database API
• Easier to define an API
• The DB becomes part of that API
Tuesday, October 20, 2009
![Page 27: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/27.jpg)
Database API
• Easier to define an API
• The DB becomes part of that API
• Simple Stored Procedure interface
Tuesday, October 20, 2009
![Page 28: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/28.jpg)
Database API
• Easier to define an API
• The DB becomes part of that API
• Simple Stored Procedure interface
• Easily declare new Exceptions
Tuesday, October 20, 2009
![Page 29: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/29.jpg)
Wonderfully Python
• A Simple Decorator
Tuesday, October 20, 2009
![Page 30: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/30.jpg)
Simply Decorated
from exceptable.exceptable import Except
base = Except()
@base
def db_function():
pass
Tuesday, October 20, 2009
![Page 31: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/31.jpg)
Wonderfully Python
• A Simple Decorator
• Catches and re-emits Exceptions
• The core of Exceptable
• Easy to Integrate - 2 lines, in Simpycity
Tuesday, October 20, 2009
![Page 32: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/32.jpg)
Universality
• Exceptable Procedures never change
• DB logic doesn’t change
• Application support is Easy
Tuesday, October 20, 2009
![Page 33: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/33.jpg)
Exceptable
• More Pythonic Database Access
• Better exceptions means better app flow
Tuesday, October 20, 2009
![Page 34: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/34.jpg)
Example CodeWherein
• The DB Library
• The Application Implementation
• Catching Permissions Violations
• PostgreSQL 8.4
Tuesday, October 20, 2009
![Page 35: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/35.jpg)
To Start,
CREATE TABLE exceptions (
name text primary key,
description text not null,
parent text references exceptions(name)
);
INSERT INTO exceptions VALUES ('Exception', 'Base exception',NULL);
INSERT INTO exceptions VALUES ('NotFoundException', 'Could not find specified record', 'Exception');
Tuesday, October 20, 2009
![Page 36: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/36.jpg)
Which leads to
CREATE OR REPLACE FUNCTION not_found ( in_reason TEXT) RETURNS VOID as $body$
SELECT exceptaple.raise( 'NotFoundException', $1 );
$body$ LANGUAGE SQL;
Tuesday, October 20, 2009
![Page 37: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/37.jpg)
Application Level
• Easy to Query the Exception tables
Tuesday, October 20, 2009
![Page 38: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/38.jpg)
Application Level
• Easy to Query the Exception tables
• Easy to set up a new library
• Python took 50 lines
Tuesday, October 20, 2009
![Page 39: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/39.jpg)
Our Python Example
• The Exceptable decorator is easy to set up
• Designed for DB-API integration
Tuesday, October 20, 2009
![Page 40: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/40.jpg)
In the Application
base = Except(InternalError, { 'Exception': Exception, 'NotFoundException': NotFoundError,})
Tuesday, October 20, 2009
![Page 41: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/41.jpg)
Our Python Example
• The Exceptable decorator is easy to set up
• Designed for DB-API integration
• User-defined
Tuesday, October 20, 2009
![Page 42: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/42.jpg)
User Definitions
base = Except(InternalError, {
'PermissionError': PermissionError,
'UnknownUser': UnknownUserError,
'NotFoundException': NotFoundError,
})
Tuesday, October 20, 2009
![Page 43: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/43.jpg)
Our Python Example
• The Exceptable decorator is easy to set up
• Designed for DB-API integration
• User-defined, and soon, table introspection
Tuesday, October 20, 2009
![Page 44: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/44.jpg)
base is a decorator
@basedef db_api(query): con = db.connect(conn_string) cur = con.cursor() return cur(query)
Tuesday, October 20, 2009
![Page 45: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/45.jpg)
Which leads to
try: rs = db_api(‘select * from test_api()’)except NotFoundError, e: # A hah! A usable error! pass
Tuesday, October 20, 2009
![Page 46: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/46.jpg)
As Opposed To
try: rs = db_api(‘select * from test_api()’)except InternalError, e: if “NotFoundException” in str(e): raise NotFoundError(str(e)) elif “PermissionsError” in str(e): raise PermissionsError(str(e))
Tuesday, October 20, 2009
![Page 47: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/47.jpg)
Our Python Example
• The Exceptable decorator is easy to set up
• Designed for DB-API integration
• User-defined, and soon, table introspection
• Existing decorators can be expanded easily
Tuesday, October 20, 2009
![Page 48: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/48.jpg)
Grow, my Pretties!
class NotNullError(BaseException): passclass SCE(BaseException): pass
base.add({‘NotNullException’: NotNullError,‘SufficientCoffeeException’: SCE
})
Tuesday, October 20, 2009
![Page 49: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/49.jpg)
Permission Denied
• Exceptable.py also wraps the base permission denied
• DB permissions violations work at the app level
Tuesday, October 20, 2009
![Page 50: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/50.jpg)
Vertically Challenged
a = c.Function(‘test_auth’);try: result = a()except PermissionDenied, e: abort(403)except NoSuchUser, e: abort(401)
Tuesday, October 20, 2009
![Page 51: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/51.jpg)
AND THUSQuestions?
Tuesday, October 20, 2009
![Page 52: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/52.jpg)
Get It!
https://projects.commandprompt.com/public/exceptable/
Tuesday, October 20, 2009
![Page 53: Exceptable - Exception Propagation for PG](https://reader036.fdocuments.in/reader036/viewer/2022062704/555a7159d8b42ae7218b5387/html5/thumbnails/53.jpg)
THANK YOU!
Tuesday, October 20, 2009