Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 1 | (* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | *) |
| 19 | |
| 20 | {$SCOPEDENUMS ON} |
| 21 | |
| 22 | unit Thrift.Exception; |
| 23 | |
| 24 | interface |
| 25 | |
| 26 | uses |
| 27 | Classes, SysUtils; |
| 28 | |
| 29 | type |
| 30 | // base class for all Thrift exceptions |
| 31 | TException = class( SysUtils.Exception) |
Jens Geyer | e780855 | 2019-12-04 21:24:08 +0100 | [diff] [blame] | 32 | strict private |
| 33 | function GetMessageText : string; |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 34 | public |
| 35 | function Message : string; // hide inherited property: allow read, but prevent accidental writes |
| 36 | procedure UpdateMessageProperty; // update inherited message property with toString() |
| 37 | end; |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | implementation |
| 43 | |
| 44 | { TException } |
| 45 | |
| 46 | function TException.Message; |
| 47 | // allow read (exception summary), but prevent accidental writes |
| 48 | // read will return the exception summary |
| 49 | begin |
Jens Geyer | e780855 | 2019-12-04 21:24:08 +0100 | [diff] [blame] | 50 | result := Self.GetMessageText; |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 51 | end; |
| 52 | |
Jens Geyer | e780855 | 2019-12-04 21:24:08 +0100 | [diff] [blame] | 53 | |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 54 | procedure TException.UpdateMessageProperty; |
| 55 | // Update the inherited Message property to better conform to standard behaviour. |
| 56 | // Nice benefit: The IDE is now able to show the exception message again. |
| 57 | begin |
Jens Geyer | e780855 | 2019-12-04 21:24:08 +0100 | [diff] [blame] | 58 | inherited Message := Self.GetMessageText; |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 59 | end; |
| 60 | |
| 61 | |
Jens Geyer | e780855 | 2019-12-04 21:24:08 +0100 | [diff] [blame] | 62 | function TException.GetMessageText : string; |
| 63 | // produces a summary text |
| 64 | begin |
| 65 | result := Self.ToString; |
| 66 | if (result <> '') and (result[1] = '(') |
| 67 | then result := Copy(result,2,Length(result)-2); |
| 68 | end; |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 69 | |
| 70 | |
| 71 | end. |
| 72 | |