File: GLib-Error.lua

package info (click to toggle)
lua-lgi 0.9.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,388 kB
  • sloc: ansic: 5,082; makefile: 169; sh: 31
file content (93 lines) | stat: -rw-r--r-- 2,579 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-------------------------------------------------------------------------------
--
-- lgi GLib Error support implementation.
--
-- Copyright (c) 2013 Pavel Holejsovsky
-- Licensed under the MIT license:
-- http://www.opensource.org/licenses/mit-license.php
--
-------------------------------------------------------------------------------

local pairs, type, select
   = pairs, type, select

local lgi = require 'lgi'
local core = require 'lgi.core'
local record = require 'lgi.record'

local GLib = lgi.GLib

local Error = lgi.GLib.Error
Error._attribute = {}

-- Add attribute which looks up associated enum type.
Error._attribute.domain = {
   get = function(object)
      local info = core.gi[core.record.field(object, Error._field.domain)]
      return core.repotype(info)
   end,
}

-- Override code attribute to return symbolic code from enum.
Error._attribute.code = {
   get = function(object)
      local code = core.record.field(object, Error._field.code)
      local enum = object.domain
      return enum and enum[code] or code
   end,
}

-- Converts domain to quark
local function domain_to_quark(domain)
   if type(domain) == 'table' then
      return domain.error_domain
   elseif type(domain) == 'string' then
      return GLib.quark_from_string(domain)
   else
      return domain
   end
end

-- Converts code to numeric code, assumes already quark-form domain
local function code_to_number(code, domain)
   if type(code) == 'string' then
      return core.repotype(core.gi[domain])(code)
   else
      return code
   end
end

-- Create new error instance.
function Error.new(domain, code, message, ...)
   domain = domain_to_quark(domain)
   code = code_to_number(code, domain)
   if select('#', ...) > 0 then
      message = message:format(...)
   end
   return Error.new_literal(domain, code, message)
end

function Error:_new(...)
   return Error.new(...)
end

-- Override _tostring, in order to automatically convert errors to
-- error messages.
function Error:_tostring()
   return self.message
end

-- Override matches() method, so that it can use different methods of
-- entering domain and/or code.
function Error:matches(domain, code)
   if GLib.Error:is_type_of(domain) then
      -- This actually means match against another error instance.
      local err = domain
      domain = core.record.field(err, GLib.Error._field.domain)
      code = core.record.field(err, GLib.Error._field.code)      
   else
      domain = domain_to_quark(domain)
      code = code_to_number(code, domain)
   end
   return Error._method.matches(self, domain, code)
end