PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/topaz/types/error.topaz

https://github.com/KoreanFoodComics/Project-SnowBall
Unknown | 174 lines | 168 code | 6 blank | 0 comment | 0 complexity | 1cfa2aa6efc2bb192051170377c7f33c MD5 | raw file
  1Topaz [
  2    Title: "Topaz types: ERROR!"
  3    Author: "Gabriele Santilli"
  4    Copyright: 2011
  5    Type: Fake-Topaz
  6    ; License: {
  7    ;     Permission is hereby granted, free of charge, to any person obtaining
  8    ;     a copy of this software and associated documentation files
  9    ;     (the "Software"), to deal in the Software without restriction, including
 10    ;     without limitation the rights to use, copy, modify, merge, publish,
 11    ;     distribute, sublicense, and/or sell copies of the Software, and to
 12    ;     permit persons to whom the Software is furnished to do so, subject
 13    ;     to the following conditions:
 14
 15    ;     The above copyright notice and this permission notice shall be included
 16    ;     in all copies or substantial portions of the Software.
 17
 18    ;     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 19    ;     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20    ;     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 21    ;     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 22    ;     OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 23    ;     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 24    ;     OTHER DEALINGS IN THE SOFTWARE.
 25    ; }
 26]
 27
 28; ===== TYPES: ERROR! =========================================================
 29
 30make-type 'error! [
 31    make: function [args] [res] [
 32        if not args [args: make-struct []]
 33        res: make-struct [
 34            type: error!
 35            category: any [args/category "Internal"]
 36            id: any [args/id "unspecified"]
 37            message: any [args/message "Unspecified error"]
 38            args: args/args
 39            stack: make block! none
 40        ]
 41        if args/stack [insert/only res/stack args/stack]
 42        res
 43    ]
 44    topaz-make: function [value] [blk] [
 45        switch-default value/type/name [
 46            "block!" [
 47                blk: make block! none
 48                append blk make none! none
 49                insert/only tail blk value
 50                topaz-make-error apply object!/topaz-make [blk]
 51            ]
 52            "object!" [
 53                topaz-make-error value
 54            ]
 55            "error!" [value]
 56        ] [
 57            apply error!/make [make-struct [
 58                category: "Script"
 59                id: "invalid-argument"
 60                message: "Invalid argument for MAKE ERROR!"
 61                args: value
 62            ]]
 63        ]
 64    ]
 65    mold: function [
 66        "Return a LOAD-able text representation of a value"
 67        err
 68        options:
 69            only: no [logic!] "Don't generate outer [ ] for block! values"
 70            flat: no [logic!] "Produce a single text line"
 71            limit [number! none!] "Don't return a string longer than LIMIT characters"
 72            indent: "" [string!] "Add this string after each new line (ignored if flat)"
 73    ] [result] [
 74        limit-string either flat [
 75            rejoin [
 76                "make error! [category: '" err/category " id: '" err/id " "
 77                "message: ^"" escape err/message "^" "
 78                either err/args [
 79                    rejoin [
 80                        "args: " mold/options [
 81                            value: either word? err/args [make lit-word! err/args/word] [err/args]
 82                            flat
 83                            limit: limit
 84                        ] " "
 85                    ]
 86                ] [""]
 87                "stack: " mold/options [value: err/stack flat limit: limit] "]"
 88            ]
 89        ] [
 90            rejoin [
 91                "make error! [^/"
 92                indent "    category: '" err/category "^/"
 93                indent "    id: '" err/id "^/"
 94                indent "    message: ^"" escape err/message "^"^/"
 95                either err/args [
 96                    rejoin [
 97                        indent "    args: " mold/options [
 98                            value: either word? err/args [make lit-word! err/args/word] [err/args]
 99                            indent: indent + "    "
100                            limit: limit
101                        ] "^/"
102                    ]
103                ] [""]
104                indent "    stack: " mold/options [value: err/stack limit: limit indent: indent + "    "] "^/"
105                indent "]"
106            ]
107        ] limit
108    ]
109    do: function [err block] [] [
110        reduce [err skip block 1]
111    ]
112    bind: :default-bind
113    compile: function [err block] [] [
114        reduce [ast-value err skip block 1]
115    ]
116    equal?: :default-equal?
117    copy: function [value] [] [value]
118]
119error!/("error!"): make-struct [
120    equal?: function [err1 err2] [] [
121        all [
122            err1/category = err2/category
123            err1/id = err2/id
124        ]
125    ]
126]
127
128topaz-make-error: function [obj] [args err] [
129    obj: obj/map
130    args: make-struct []
131    if obj/category [
132        args/category: either obj/category/type/name = "word!" [obj/category/word] [none]
133    ]
134    if obj/id [
135        args/id: either obj/id/type/name = "word!" [obj/id/word] [none]
136    ]
137    if obj/message [
138        args/message: either obj/message/type/name = "string!" [obj/message/string] [none]
139    ]
140    if all [obj/args not none? obj/args] [
141        args/args: obj/args
142    ]
143    err: apply error!/make [args]
144    if all [obj/stack block? obj/stack] [
145        err/stack: obj/stack
146    ]
147    err
148]
149
150error: function [args] [] [
151    throw apply error!/make [args]
152]
153form-error: function [err] [res] [
154    res: rejoin [
155        "*** " err/category " error: " err/message
156        either err/args [
157            either all [err/category = "Internal" string? err/args] [
158                ; JS error
159                rejoin [": " err/args/string]
160            ] [
161                rejoin [": " mold/options [value: err/args flat limit: 80]]
162            ]
163        ] [""]
164    ]
165    foreach 'item err/stack/values [
166        either all [err/category = "Internal" string? item] [
167            ; JS error
168            res: rejoin [res "^/*** JS Stack:^/" item/string "^/"]
169        ] [
170            res: rejoin [res "^/*** Stack: " mold/options [value: item only limit: 160 indent: "         : "]]
171        ]
172    ]
173    res
174]