lux-fw 0.5.36 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/bin/README.md +26 -13
  4. data/bin/cli/benchmark.rb +18 -0
  5. data/bin/cli/cerb.rb +90 -0
  6. data/bin/cli/config.rb +4 -11
  7. data/bin/cli/console.rb +72 -30
  8. data/bin/cli/eval.rb +5 -27
  9. data/bin/cli/generate.rb +9 -11
  10. data/bin/cli/get.rb +14 -6
  11. data/bin/cli/memory.rb +12 -0
  12. data/bin/cli/new.rb +2 -0
  13. data/bin/cli/secrets.rb +3 -31
  14. data/bin/cli/server.rb +28 -9
  15. data/bin/cli/stats.rb +26 -20
  16. data/bin/cli/template.rb +10 -0
  17. data/bin/generate_doc +168 -0
  18. data/bin/lux +73 -10
  19. data/lib/README.md +18 -8
  20. data/lib/common/crypt.rb +60 -9
  21. data/lib/common/string_base.rb +54 -15
  22. data/lib/common/struct_opts.rb +30 -0
  23. data/lib/common/time_difference.rb +75 -0
  24. data/lib/loader.rb +41 -0
  25. data/lib/lux/application/README.md +67 -105
  26. data/lib/lux/application/application.rb +87 -329
  27. data/lib/lux/application/lib/magic_routes.rb +22 -0
  28. data/lib/lux/application/lib/nav.rb +218 -99
  29. data/lib/lux/application/lib/render.rb +47 -67
  30. data/lib/lux/application/lib/routes.rb +242 -0
  31. data/lib/lux/application/lib/shared.rb +47 -0
  32. data/lib/lux/application/lux_adapter.rb +6 -0
  33. data/lib/lux/application/lux_config.rb +26 -0
  34. data/lib/lux/cache/Find Results +24 -0
  35. data/lib/lux/cache/README.md +28 -35
  36. data/lib/lux/cache/cache.rb +78 -26
  37. data/lib/lux/cache/lib/memcached_server.rb +36 -0
  38. data/lib/lux/cache/lib/memory_server.rb +44 -0
  39. data/lib/lux/cache/lib/null_server.rb +33 -0
  40. data/lib/lux/cache/lib/sqlite_server.rb +62 -0
  41. data/lib/lux/cache/lux_adapter.rb +19 -0
  42. data/lib/lux/config/config.rb +93 -144
  43. data/lib/lux/config/lux_adapter.rb +27 -0
  44. data/lib/lux/controller/README.md +67 -12
  45. data/lib/lux/controller/controller.rb +237 -187
  46. data/lib/lux/current/README.md +29 -10
  47. data/lib/lux/current/current.rb +166 -77
  48. data/lib/lux/current/lib/current.rb +6 -0
  49. data/lib/lux/current/lib/encrypt_params.rb +38 -36
  50. data/lib/lux/current/lib/session.rb +86 -65
  51. data/lib/lux/current/lux_adapter.rb +5 -0
  52. data/lib/lux/environment/README.md +24 -0
  53. data/lib/lux/environment/environment.rb +91 -0
  54. data/lib/lux/environment/lux_adapter.rb +13 -0
  55. data/lib/lux/error/README.md +18 -19
  56. data/lib/lux/error/error.rb +207 -152
  57. data/lib/lux/error/lux_adapter.rb +9 -0
  58. data/lib/lux/logger/README.md +45 -0
  59. data/lib/lux/logger/lux_adapter.rb +27 -0
  60. data/lib/lux/lux.rb +72 -129
  61. data/lib/lux/mailer/README.md +12 -5
  62. data/lib/lux/mailer/lux_config.rb +4 -0
  63. data/lib/lux/mailer/mailer.rb +81 -59
  64. data/lib/lux/plugin/README.md +19 -0
  65. data/lib/lux/plugin/lux_adapter.rb +8 -0
  66. data/lib/lux/plugin/plugin.rb +65 -0
  67. data/lib/lux/render/README.md +100 -0
  68. data/lib/lux/render/render.rb +12 -0
  69. data/lib/lux/response/README.md +57 -1
  70. data/lib/lux/response/lib/file.rb +87 -65
  71. data/lib/lux/response/lib/flash.rb +49 -34
  72. data/lib/lux/response/lib/header.rb +34 -33
  73. data/lib/lux/response/response.rb +252 -164
  74. data/lib/lux/template/helper.rb +130 -0
  75. data/lib/lux/template/inline_render_proxy.rb +31 -0
  76. data/lib/lux/template/lux_adapter.rb +5 -0
  77. data/lib/lux/template/template.rb +127 -0
  78. data/lib/lux-fw.rb +15 -33
  79. data/lib/overload/array.rb +33 -7
  80. data/lib/overload/blank.rb +8 -3
  81. data/lib/overload/boolean.rb +46 -0
  82. data/lib/overload/class.rb +28 -0
  83. data/lib/overload/dir.rb +117 -7
  84. data/lib/overload/file.rb +40 -5
  85. data/lib/overload/float.rb +38 -3
  86. data/lib/overload/hash.rb +81 -91
  87. data/lib/overload/integer.rb +15 -0
  88. data/lib/overload/json.rb +26 -0
  89. data/lib/overload/nil.rb +4 -8
  90. data/lib/overload/object.rb +91 -24
  91. data/lib/overload/pathname.rb +11 -0
  92. data/lib/overload/raise_variants.rb +26 -36
  93. data/lib/overload/string.rb +100 -27
  94. data/lib/overload/thread_simple.rb +100 -0
  95. data/lib/overload/time.rb +60 -38
  96. data/tasks/loader.rb +20 -7
  97. metadata +170 -169
  98. data/bin/.DS_Store +0 -0
  99. data/bin/build_gem +0 -77
  100. data/bin/cli/dbconsole.rb +0 -8
  101. data/bin/cli/routes.rb +0 -90
  102. data/lib/.DS_Store +0 -0
  103. data/lib/common/.DS_Store +0 -0
  104. data/lib/common/class_attributes.rb +0 -51
  105. data/lib/common/class_callbacks.rb +0 -47
  106. data/lib/common/free_struct.rb +0 -42
  107. data/lib/common/hash_with_indifferent_access.rb +0 -74
  108. data/lib/common/html_tag_builder.rb +0 -91
  109. data/lib/common/method_attr.rb +0 -69
  110. data/lib/common/url.rb +0 -191
  111. data/lib/lux/.DS_Store +0 -0
  112. data/lib/lux/README.md +0 -10
  113. data/lib/lux/cache/.DS_Store +0 -0
  114. data/lib/lux/cache/lib/memory.rb +0 -36
  115. data/lib/lux/cache/lib/null.rb +0 -21
  116. data/lib/lux/config/README.md +0 -63
  117. data/lib/lux/config/lib/plugin.rb +0 -65
  118. data/lib/lux/config/lib/secrets.rb +0 -48
  119. data/lib/lux/current/.DS_Store +0 -0
  120. data/lib/lux/delayed_job/.DS_Store +0 -0
  121. data/lib/lux/delayed_job/README.md +0 -3
  122. data/lib/lux/delayed_job/delayed_job.rb +0 -51
  123. data/lib/lux/delayed_job/lib/memory.rb +0 -16
  124. data/lib/lux/delayed_job/lib/nsq.rb +0 -3
  125. data/lib/lux/delayed_job/lib/postgre.rb +0 -6
  126. data/lib/lux/delayed_job/lib/redis.rb +0 -19
  127. data/lib/lux/event_bus/README.md +0 -36
  128. data/lib/lux/event_bus/event_bus.rb +0 -27
  129. data/lib/lux/view/README.md +0 -85
  130. data/lib/lux/view/cell.rb +0 -102
  131. data/lib/lux/view/helper.rb +0 -120
  132. data/lib/lux/view/lib/cell_helpers.rb +0 -29
  133. data/lib/lux/view/view.rb +0 -95
  134. data/lib/overload/it.rb +0 -29
  135. data/plugins/api/README.md +0 -49
  136. data/plugins/api/api.rb +0 -153
  137. data/plugins/api/lib/attr.rb +0 -31
  138. data/plugins/api/lib/dsl.rb +0 -52
  139. data/plugins/api/lib/error.rb +0 -3
  140. data/plugins/api/lib/model_api.rb +0 -203
  141. data/plugins/api/lib/response.rb +0 -85
  142. data/plugins/db/.DS_Store +0 -0
  143. data/plugins/db/README.md +0 -29
  144. data/plugins/db/auto_migrate/auto_migrate.rb +0 -268
  145. data/plugins/db/auto_migrate/db.rake +0 -15
  146. data/plugins/db/helpers/array_search.rb +0 -27
  147. data/plugins/db/helpers/before_save_filters.rb +0 -32
  148. data/plugins/db/helpers/composite_primary_keys.rb +0 -36
  149. data/plugins/db/helpers/core.rb +0 -94
  150. data/plugins/db/helpers/dataset_methods.rb +0 -138
  151. data/plugins/db/helpers/enums_plugin.rb +0 -52
  152. data/plugins/db/helpers/find_precache.rb +0 -31
  153. data/plugins/db/helpers/link_objects.rb +0 -84
  154. data/plugins/db/helpers/schema_checks.rb +0 -83
  155. data/plugins/db/helpers/typero_adapter.rb +0 -71
  156. data/plugins/db/logger/config.rb +0 -22
  157. data/plugins/db/logger/lux_response_adapter.rb +0 -10
  158. data/plugins/db/paginate/helper.rb +0 -32
  159. data/plugins/db/paginate/sequel_adapter.rb +0 -23
  160. data/plugins/exceptions/exceptions.rake +0 -43
  161. data/plugins/exceptions/simple_exception.rb +0 -64
  162. data/plugins/favicon/favicon.rb +0 -10
  163. data/plugins/html/README.md +0 -3
  164. data/plugins/html/html_form.rb +0 -118
  165. data/plugins/html/html_input.rb +0 -98
  166. data/plugins/html/html_menu.rb +0 -79
  167. data/plugins/html/input_types.rb +0 -346
  168. data/plugins/js_widgets/README.md +0 -5
  169. data/plugins/js_widgets/js/html_tag.coffee +0 -42
  170. data/plugins/js_widgets/js/widgets.coffee +0 -161
  171. data/plugins/js_widgets/js_widgets.rb +0 -15
  172. data/plugins/oauth/lib/facebook.rb +0 -35
  173. data/plugins/oauth/lib/github.rb +0 -38
  174. data/plugins/oauth/lib/google.rb +0 -41
  175. data/plugins/oauth/lib/linkedin.rb +0 -41
  176. data/plugins/oauth/lib/stackexchange.rb +0 -41
  177. data/plugins/oauth/lib/twitter.rb +0 -38
  178. data/plugins/oauth/oauth.rb +0 -42
  179. data/plugins/policy/policy.rb +0 -53
  180. data/tasks/nginx.rake +0 -23
  181. /data/lib/lux/{view/lib → mailer}/helper_modules.rb +0 -0
@@ -1,41 +1,40 @@
1
- ## Lux::Errors - In case of error
1
+ ## Lux.error (Lux::Error)
2
2
 
3
- ### module Lux::Error
3
+ Error handling module.
4
4
 
5
5
  ```ruby
6
- # try to execute part of the code, log exeception if fails
7
- def try(name, &block)
6
+ # try to execute part of the code, log exeception if fails
7
+ Lux.error.try(name, &block)
8
8
 
9
- # HTML render style for default Lux error
10
- def render(desc)
9
+ # HTML render style for default Lux error
10
+ Lux.error.render(desc)
11
11
 
12
- # show error page
13
- def show(desc)
12
+ # show error page
13
+ Lux.error.show(desc)
14
14
 
15
- # show inline error
16
- def inline(name=nil, o=nil)
15
+ # show inline error
16
+ Lux.error.inline(name=nil, error_object=nil)
17
17
 
18
- # log exeption
19
- def log(exp_object)
18
+ # log exeption via Lux.config.log_exception_via method
19
+ Lux.error.log(error_object)
20
20
  ```
21
21
 
22
22
 
23
- ### defines standard Lux errors and erro generating helpers
23
+ #### defines standard Lux errors and error generating helpers
24
24
 
25
25
  ```ruby
26
26
  # 400: for bad parameter request or similar
27
- Lux::Error.forbidden foo
27
+ Lux.error.forbidden foo
28
28
 
29
29
  # 401: for unauthorized access
30
- Lux::Error.forbidden foo
30
+ Lux.error.forbidden foo
31
31
 
32
32
  # 403: for unalloed access
33
- Lux::Error.forbidden foo
33
+ Lux.error.forbidden foo
34
34
 
35
35
  # 404: for not found pages
36
- Lux::Error.not_found foo
36
+ Lux.error.not_found foo
37
37
 
38
38
  # 503: for too many requests at the same time
39
- Lux::Error.forbidden foo
40
-
39
+ Lux.error.forbidden foo
41
40
  ```
@@ -10,184 +10,239 @@
10
10
  # e = Lux::Error.not_found('foo')
11
11
  # e.code => 404
12
12
  # e.message => foo
13
-
14
- class Lux::Error < StandardError
15
- class AutoRaise < Lux::Error
16
- end
17
-
18
- # https://httpstatuses.com/
19
- CODE_LIST ||= {
20
- # 1×× Informational
21
- 100 => { name: 'Continue' },
22
- 101 => { name: 'Switching Protocols' },
23
- 102 => { name: 'Processing' },
24
-
25
- # 2×× Success
26
- 200 => { name: 'OK' },
27
- 201 => { name: 'Created' },
28
- 202 => { name: 'Accepted' },
29
- 203 => { name: 'Non-authoritative Information' },
30
- 204 => { name: 'No Content' },
31
- 205 => { name: 'Reset Content' },
32
- 206 => { name: 'Partial Content' },
33
- 207 => { name: 'Multi-Status' },
34
- 208 => { name: 'Already Reported' },
35
- 226 => { name: 'IM Used' },
36
-
37
- # 3×× Redirection
38
- 300 => { name: 'Multiple Choices' },
39
- 301 => { name: 'Moved Permanently' },
40
- 302 => { name: 'Found' },
41
- 303 => { name: 'See Other' },
42
- 304 => { name: 'Not Modified' },
43
- 305 => { name: 'Use Proxy' },
44
- 307 => { name: 'Temporary Redirect' },
45
- 308 => { name: 'Permanent Redirect' },
46
-
47
- # 4×× Client Error
48
- 400 => { name: 'Bad Request', code: :bad_request },
49
- 401 => { name: 'Unauthorized', code: :unauthorized },
50
- 402 => { name: 'Payment Required', code: :payment_required },
51
- 403 => { name: 'Forbidden', code: :forbidden },
52
- 404 => { name: 'Document Not Found', code: :not_found },
53
- 405 => { name: 'Method Not Allowed', code: :method_not_allowed },
54
- 406 => { name: 'Not Acceptable', code: :not_acceptable },
55
- 407 => { name: 'Proxy Authentication Required' },
56
- 408 => { name: 'Request Timeout' },
57
- 409 => { name: 'Conflict' },
58
- 410 => { name: 'Gone' },
59
- 411 => { name: 'Length Required' },
60
- 412 => { name: 'Precondition Failed' },
61
- 413 => { name: 'Payload Too Large' },
62
- 414 => { name: 'Request-URI Too Long' },
63
- 415 => { name: 'Unsupported Media Type' },
64
- 416 => { name: 'Requested Range Not Satisfiable' },
65
- 417 => { name: 'Expectation Failed' },
66
- 418 => { name: 'I\'m a teapot' },
67
- 421 => { name: 'Misdirected Request' },
68
- 422 => { name: 'Unprocessable Entity' },
69
- 423 => { name: 'Locked' },
70
- 424 => { name: 'Failed Dependency' },
71
- 426 => { name: 'Upgrade Required' },
72
- 428 => { name: 'Precondition Required' },
73
- 429 => { name: 'Too Many Requests' },
74
- 431 => { name: 'Request Header Fields Too Large' },
75
- 444 => { name: 'Connection Closed Without Response' },
76
- 451 => { name: 'Unavailable For Legal Reasons' },
77
- 499 => { name: 'Client Closed Request' },
78
-
79
- # 5×× Server Error
80
- 500 => { name: 'Internal Server Error', code: :internal_server_error },
81
- 501 => { name: 'Not Implemented', code: :not_implemented },
82
- 502 => { name: 'Bad Gateway' },
83
- 503 => { name: 'Service Unavailable' },
84
- 504 => { name: 'Gateway Timeout' },
85
- 505 => { name: 'HTTP Version Not Supported' },
86
- 506 => { name: 'Variant Also Negotiates' },
87
- 507 => { name: 'Insufficient Storage' },
88
- 508 => { name: 'Loop Detected' },
89
- 510 => { name: 'Not Extended' },
90
- 511 => { name: 'Network Authentication Required' },
91
- 599 => { name: 'Network Connect Timeout Error' },
92
- }
93
-
94
- # e = Lux::Error.not_found('foo')
95
- CODE_LIST.each do |status, data|
96
- if data[:code]
97
- define_singleton_method(data[:code]) do |message=nil|
98
- error = new status, message
99
- raise error if Lux::Error::AutoRaise === error
100
- error
101
- end
13
+ module Lux
14
+ class Error < StandardError
15
+ class AutoRaise < Lux::Error
102
16
  end
103
- end
104
17
 
105
- class << self
106
- # template to show full error page
107
- def render text, status=500
108
- Lux.current.response.status status
109
- Lux.current.response.body Lux.config.server_error_template.call(text)
110
- throw :done
18
+ # https://httpstatuses.com/
19
+ CODE_LIST ||= {
20
+ # 1×× Informational
21
+ 100 => { name: 'Continue' },
22
+ 101 => { name: 'Switching Protocols' },
23
+ 102 => { name: 'Processing' },
24
+
25
+ # 2×× Success
26
+ 200 => { name: 'OK' },
27
+ 201 => { name: 'Created' },
28
+ 202 => { name: 'Accepted' },
29
+ 203 => { name: 'Non-authoritative Information' },
30
+ 204 => { name: 'No Content' },
31
+ 205 => { name: 'Reset Content' },
32
+ 206 => { name: 'Partial Content' },
33
+ 207 => { name: 'Multi-Status' },
34
+ 208 => { name: 'Already Reported' },
35
+ 226 => { name: 'IM Used' },
36
+
37
+ # 3×× Redirection
38
+ 300 => { name: 'Multiple Choices' },
39
+ 301 => { name: 'Moved Permanently' },
40
+ 302 => { name: 'Found' },
41
+ 303 => { name: 'See Other' },
42
+ 304 => { name: 'Not Modified' },
43
+ 305 => { name: 'Use Proxy' },
44
+ 307 => { name: 'Temporary Redirect' },
45
+ 308 => { name: 'Permanent Redirect' },
46
+
47
+ # 4×× Client Error
48
+ 400 => { name: 'Bad Request', short: :bad_request },
49
+ 401 => { name: 'Unauthorized', short: :unauthorized },
50
+ 402 => { name: 'Payment Required', short: :payment_required },
51
+ 403 => { name: 'Forbidden', short: :forbidden },
52
+ 404 => { name: 'Document Not Found', short: :not_found },
53
+ 405 => { name: 'Method Not Allowed', short: :method_not_allowed },
54
+ 406 => { name: 'Not Acceptable', short: :not_acceptable },
55
+ 407 => { name: 'Proxy Authentication Required' },
56
+ 408 => { name: 'Request Timeout' },
57
+ 409 => { name: 'Conflict' },
58
+ 410 => { name: 'Gone' },
59
+ 411 => { name: 'Length Required' },
60
+ 412 => { name: 'Precondition Failed' },
61
+ 413 => { name: 'Payload Too Large' },
62
+ 414 => { name: 'Request-URI Too Long' },
63
+ 415 => { name: 'Unsupported Media Type' },
64
+ 416 => { name: 'Requested Range Not Satisfiable' },
65
+ 417 => { name: 'Expectation Failed' },
66
+ 418 => { name: 'I\'m a teapot' },
67
+ 421 => { name: 'Misdirected Request' },
68
+ 422 => { name: 'Unprocessable Entity' },
69
+ 423 => { name: 'Locked' },
70
+ 424 => { name: 'Failed Dependency' },
71
+ 426 => { name: 'Upgrade Required' },
72
+ 428 => { name: 'Precondition Required' },
73
+ 429 => { name: 'Too Many Requests' },
74
+ 431 => { name: 'Request Header Fields Too Large' },
75
+ 444 => { name: 'Connection Closed Without Response' },
76
+ 451 => { name: 'Unavailable For Legal Reasons' },
77
+ 499 => { name: 'Client Closed Request' },
78
+
79
+ # 5×× Server Error
80
+ 500 => { name: 'Internal Server Error', short: :internal_server_error },
81
+ 501 => { name: 'Not Implemented', short: :not_implemented },
82
+ 502 => { name: 'Bad Gateway' },
83
+ 503 => { name: 'Service Unavailable' },
84
+ 504 => { name: 'Gateway Timeout' },
85
+ 505 => { name: 'HTTP Version Not Supported' },
86
+ 506 => { name: 'Variant Also Negotiates' },
87
+ 507 => { name: 'Insufficient Storage' },
88
+ 508 => { name: 'Loop Detected' },
89
+ 510 => { name: 'Not Extended' },
90
+ 511 => { name: 'Network Authentication Required' },
91
+ 599 => { name: 'Network Connect Timeout Error' },
92
+ }
93
+
94
+ # e = Lux::Error.not_found('foo')
95
+ CODE_LIST.each do |status, data|
96
+ if data[:short]
97
+ define_singleton_method(data[:short]) do |message=nil|
98
+ error = Lux::Error.new status, message
99
+
100
+ if self == Lux::Error::AutoRaise
101
+ Lux.current.response.status status
102
+ Lux.log " error.#{data[:code]} in #{Lux.app_caller}"
103
+ raise error
104
+ end
105
+
106
+ error
107
+ end
108
+ end
111
109
  end
112
110
 
113
- # render error inline or break in production
114
- def inline name, error=nil
115
- error ||= $!
111
+ class << self
112
+ # template to show full error page
113
+ def render error
114
+ error = StandardError.new(error) if error.is_a?(String)
115
+
116
+ code = error.respond_to?(:code) ? error.code : 500
117
+ Lux.current.response.status code
118
+
119
+ Lux.current.response.body(
120
+ HtmlTag.html do |n|
121
+ n.head do |n|
122
+ n.title 'Lux error'
123
+ end
124
+ n.body style: "margin: 20px 20px 20px 140px; background-color:#fdd; font-size: 14pt; font-family: sans-serif;" do |n|
125
+ n.img src: "https://i.imgur.com/Zy7DLXU.png", style: "width: 100px; position: absolute; margin-left: -120px;"
126
+ n.h4 do |n|
127
+ n.push %[HTTP Error &mdash; <a href="https://httpstatuses.com/#{code}" target="http_error">#{code}</a>]
128
+ n.push %[ &dash; #{error.name}] if error.respond_to?(:name)
129
+ end
130
+ n.push inline error
131
+ end
132
+ end
133
+ )
134
+ end
116
135
 
117
- unless Lux.config(:dump_errors)
118
- key = log error
119
- render "Lux inline error: %s\n\nkey: %s" % [error.message, key]
136
+ # render error inline
137
+ def inline object, msg = nil
138
+ error, message = if object.is_a?(String)
139
+ [nil, object]
140
+ else
141
+ [object, object.message]
142
+ end
143
+
144
+ error_key = error ? log(error) : nil
145
+ message = message.to_s.gsub('","',%[",\n "]).gsub('<','&lt;')
146
+
147
+ HtmlTag.pre(class: 'lux-inline-error', style: 'background: #fff; margin-top: 10px; padding: 10px; font-size: 14px; border: 2px solid #600; line-height: 20px;') do |n|
148
+ n.h3 '%s : %s' % [error.class, message]
149
+ n.p msg if msg
150
+ n.p 'Key: %s' % error_key if error_key
151
+ n.p 'Description: %s' % error.description if error && error.respond_to?(:description) && error.description
152
+
153
+ if error && Lux.env.show_errors?
154
+ n.hr
155
+ n.push mark_backtrace(error, html: true).join("\n")
156
+ end
157
+ end
120
158
  end
121
159
 
122
- name ||= 'Undefined name'
123
- msg = error.message.to_s.gsub('","',%[",\n "]).gsub('<','&lt;')
160
+ # clear osx screen :)
161
+ def clear_screen time = 0
162
+ last = (Thread.current[:_lux_clear_screen] ||= 1.day.ago)
163
+ if last < Time.now - time
164
+ Thread.current[:_lux_clear_screen] = Time.now
165
+ print "\e[H\e[2J\e[3J"
166
+ end
167
+ end
124
168
 
125
- dmp = split_backtrace error
169
+ # prepare backtrace for better render
170
+ def split_backtrace error
171
+ # split app log rest of the log
172
+ dmp = [[error.class, error.message], [], []]
126
173
 
127
- dmp[0] = dmp[0].map { |_| _ = _.split(':', 3); '<b>%s</b> - %s - %s' % _ }
174
+ root = Lux.root.to_s
128
175
 
129
- log error
176
+ (error.backtrace || caller).each do |line|
177
+ line = line.sub(root, '.')
178
+ dmp[line[0,1] == '.' ? 1 : 2].push line
179
+ end
130
180
 
131
- <<~TEXT
132
- <pre style="color:red; background:#eee; padding:10px; font-family:'Lucida Console'; line-height:15pt; font-size:11pt;">
133
- <b style="font-size:110%;">#{name}</b>
181
+ dmp
182
+ end
134
183
 
135
- <b>#{error}: #{msg}</b>
184
+ def mark_backtrace error, html: false
185
+ return ['no backtrace present'] unless error && error.backtrace
136
186
 
137
- #{dmp[0].join("\n")}
187
+ root = Lux.root.to_s
138
188
 
139
- #{dmp[1].join("\n")}
140
- </pre>
141
- TEXT
142
- end
189
+ error
190
+ .backtrace
191
+ .map do |line|
192
+ path = line.split(':in').first
193
+ path = path.sub(/^\.\//, root+'/')
194
+
195
+ edit = html ? %[ &bull; <a href="subl://open?url=file:/#{path}">edit</a>] : ''
196
+ line = line.sub(root, '.')
197
+ (line[0,1] != '/' ? (html ? line.tag(:b) : line) : line) + edit
198
+ end
199
+ end
143
200
 
144
- def report code, msg=nil
145
- e = Integer === code ? Lux::Error.new(code) : Lux::Error.send(code)
146
- e.message = msg if msg
147
- raise e
148
- end
201
+ # show in stdout
202
+ def screen error
203
+ return unless Lux.env.show_errors?
204
+
205
+ data = split_backtrace(error)
206
+ if error.class == Lux::Error
207
+ Lux.info "Lux error: #{error.message} (#{error.code}) - #{data[1][0]}"
208
+ else
209
+ data[2] = data[2][0,5]
210
+ ap data
211
+ end
212
+ end
149
213
 
150
- def log error
151
- Lux.config.error_logger.call error
214
+ def log err
215
+ if Lux.config.error_logger
216
+ Lux.config.error_logger.call err
217
+ end
218
+ end
152
219
  end
153
220
 
154
- def split_backtrace error
155
- # split app log rest of the log
156
- dmp = [[], []]
221
+ ###
157
222
 
158
- root = Lux.root.to_s
223
+ attr_accessor :message
159
224
 
160
- error.backtrace.each do |line|
161
- line = line.sub(root, '.')
162
- dmp[line[0,1] == '.' ? 0 : 1].push line
225
+ def initialize *args
226
+ if args.first.is_a?(Integer)
227
+ self.code = args.shift
163
228
  end
164
229
 
165
- dmp
230
+ self.message = args.shift || name
166
231
  end
167
- end
168
-
169
- ###
170
-
171
- attr_accessor :message
172
-
173
- def initialize code_num, message=nil
174
- self.code = code_num
175
- @message = message || CODE_LIST[code_num][:name]
176
- end
177
232
 
178
- def code
179
- # 400 is a default
180
- @code || 400
181
- end
233
+ def code
234
+ # 400 is a default
235
+ @code || 400
236
+ end
182
237
 
183
- def code= num
184
- @code = num.to_i
238
+ def name
239
+ CODE_LIST[code][:name]
240
+ end
185
241
 
186
- raise 'Status code %s not found' % @code unless CODE_LIST[@code]
187
- end
242
+ def code= num
243
+ @code = num.to_i
188
244
 
189
- def render
190
- self.class.render message, code
245
+ raise 'Status code %s not found' % @code unless CODE_LIST[@code]
246
+ end
191
247
  end
192
248
  end
193
-
@@ -0,0 +1,9 @@
1
+ module Lux
2
+ def error *args
3
+ if args.first
4
+ raise Lux::Error::AutoRaise.new(*args)
5
+ else
6
+ Lux::Error::AutoRaise
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ ## Lux.logger
2
+
3
+ Lux logger is logging helper module.
4
+
5
+ It has 2 basic methods, get pointer to a logger `Lux.logger(:name).info msg` and log to screen `Lux.log msg`
6
+
7
+ Uses default [Ruby logger](https://ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger.html)
8
+
9
+ ### Options
10
+
11
+ #### Lux.config.logger_path_mask
12
+
13
+ Defaults to `./log/%s.log`.
14
+
15
+ #### Lux.config.logger_formatter
16
+
17
+ If defined it will be assigned to all logs created by `Lux#logger`
18
+
19
+ #### Lux.config.logger_default
20
+
21
+ Default system logger output location called via `Lux#log`.
22
+
23
+ Defauls to `STDOUT` in development and `nil` in production (no render output log)
24
+
25
+ #### Lux.config.logger_files_to_keep = 3
26
+
27
+ By default keep 3 log files
28
+
29
+ #### Lux.config.logger_file_max_size = 10_240_000
30
+
31
+ 10 MB per file log file
32
+
33
+ ### Defaults and example
34
+
35
+ ```ruby
36
+ # defaults
37
+ Lux.config.logger_path_mask = './tmp/%s.log'
38
+ Lux.config.logger_default = Lux.env.dev? ? STDOUT : nil
39
+ Lux.config.logger_files_to_keep = 3
40
+ Lux.config.logger_file_max_size = 10_240_000
41
+
42
+ # example
43
+ # by default writes in ./log/%s.log, log rotation 3 files, 10 MB each.
44
+ Lux.logger(:foo).info 'hello'
45
+ ```
@@ -0,0 +1,27 @@
1
+ require 'logger'
2
+
3
+ module Lux
4
+ LOGGER_CACHE ||= {}
5
+
6
+ # Lux.logger(:foo).warn 'bar'
7
+ def logger name = nil
8
+ raise "Logger name is required" unless name
9
+
10
+ LOGGER_CACHE[name] ||= begin
11
+ output_location = Lux.config.logger_path_mask % name
12
+ LOGGER_CACHE[name] = Logger.new output_location, Lux.config.logger_files_to_keep, Lux.config.logger_file_max_size
13
+
14
+ if Lux.config.logger_formatter
15
+ LOGGER_CACHE[name].formatter = Lux.config.logger_formatter
16
+ end
17
+ LOGGER_CACHE[name]
18
+ end
19
+ end
20
+
21
+ def log what = nil, &block
22
+ return unless Lux.env.screen_log?
23
+ # return if Lux.current.nav.format && !Lux.current.no_cache?
24
+ what = block.call if block
25
+ print what.to_s + "\n" if what
26
+ end
27
+ end