Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
b41994a4
Unverified
Commit
b41994a4
authored
5 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
Clearer implementation of catch-exceptions QP middleware
parent
074cc50f
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/query_processor/middleware/catch_exceptions.clj
+47
-30
47 additions, 30 deletions
src/metabase/query_processor/middleware/catch_exceptions.clj
src/metabase/query_processor/middleware/log.clj
+0
-1
0 additions, 1 deletion
src/metabase/query_processor/middleware/log.clj
with
47 additions
and
31 deletions
src/metabase/query_processor/middleware/catch_exceptions.clj
+
47
−
30
View file @
b41994a4
...
...
@@ -2,31 +2,41 @@
"Middleware for catching exceptions thrown by the query processor and returning them in a friendlier format."
(
:require
[
metabase.util
:as
u
]
schema.utils
)
(
:import
[
schema.utils
NamedError
ValidationError
]))
(
:import
clojure.lang.ExceptionInfo
[
schema.utils
NamedError
ValidationError
]))
(
def
^
:dynamic
^
:private
*add-preprocessed-queries?*
true
)
(
defn-
fail
[{
query-type
:type,
:as
query
}
,
^
Throwable
e,
&
[
additional-info
]]
(
merge
{
:status
:failed
:class
(
class
e
)
:error
(
or
(
.getMessage
e
)
(
str
e
))
:stacktrace
(
u/filtered-stacktrace
e
)
;; TODO - removing this stuff is not really needed anymore since `:database` is just the ID and not the
;; entire map including `:details`
:query
(
dissoc
query
:database
:driver
)}
;; add the fully-preprocessed and native forms to the error message for MBQL queries, since they're extremely
;; useful for debugging purposes. Since generating them requires us to recursively run the query processor,
;; make sure we can skip adding them if we end up back here so we don't recurse forever
(
when
(
and
(
=
(
keyword
query-type
)
:query
)
*add-preprocessed-queries?*
)
(
binding
[
*add-preprocessed-queries?*
false
]
{
:preprocessed
(
u/ignore-exceptions
((
resolve
'metabase.query-processor/query->preprocessed
)
query
))
:native
(
u/ignore-exceptions
((
resolve
'metabase.query-processor/query->native
)
query
))}))
(
when-let
[
data
(
ex-data
e
)]
{
:ex-data
(
dissoc
data
:schema
)})
additional-info
))
(
defmulti
^
:private
format-exception
"Format an Exception thrown by the Query Processor."
{
:arglists
'
([
query
e
])}
(
fn
[
_
e
]
(
class
e
)))
(
defmethod
format-exception
Throwable
[{
query-type
:type,
:as
query
}
,
^
Throwable
e
]
(
merge
{
:status
:failed
:class
(
class
e
)
:error
(
or
(
.getMessage
e
)
(
str
e
))
:stacktrace
(
u/filtered-stacktrace
e
)
;; TODO - removing this stuff is not really needed anymore since `:database` is just the ID and not the
;; entire map including `:details`
:query
(
dissoc
query
:database
:driver
)}
;; add the fully-preprocessed and native forms to the error message for MBQL queries, since they're extremely
;; useful for debugging purposes. Since generating them requires us to recursively run the query processor,
;; make sure we can skip adding them if we end up back here so we don't recurse forever
(
when
(
and
(
=
(
keyword
query-type
)
:query
)
*add-preprocessed-queries?*
)
;; obviously we do not want to get core.async channels back for preprocessed & native, so run the preprocessing
;; steps synchronously
(
let
[
query
(
dissoc
query
:async?
)]
(
binding
[
*add-preprocessed-queries?*
false
]
{
:preprocessed
(
u/ignore-exceptions
((
resolve
'metabase.query-processor/query->preprocessed
)
query
))
:native
(
u/ignore-exceptions
((
resolve
'metabase.query-processor/query->native
)
query
))})))))
(
defn-
explain-schema-validation-error
"Return a nice error message to explain the schema validation error."
...
...
@@ -61,15 +71,22 @@
msg
)))
explanation
))))
(
defmethod
format-exception
ExceptionInfo
[
query
e
]
(
let
[{
error
:error,
error-type
:type,
:as
data
}
(
ex-data
e
)]
(
merge
((
get-method
format-exception
Throwable
)
query
e
)
(
when-let
[
error-msg
(
and
(
=
error-type
:schema.core/error
)
(
explain-schema-validation-error
error
))]
{
:error
error-msg
})
{
:ex-data
(
dissoc
data
:schema
)})))
(
defn
catch-exceptions
"Middleware for catching exceptions thrown by the query processor and returning them in a normal format."
[
qp
]
(
fn
[
query
]
(
try
(
qp
query
)
(
catch
clojure.lang.ExceptionInfo
e
(
let
[{
error
:error,
error-type
:type,
:as
data
}
(
ex-data
e
)]
(
fail
query
e
(
when-let
[
error-msg
(
and
(
=
error-type
:schema.core/error
)
(
explain-schema-validation-error
error
))]
{
:error
error-msg
}))))
(
catch
Throwable
e
(
fail
query
e
)))))
(
try
(
qp
query
)
(
catch
Throwable
e
(
format-exception
query
e
)))))
This diff is collapsed.
Click to expand it.
src/metabase/query_processor/middleware/log.clj
+
0
−
1
View file @
b41994a4
...
...
@@ -2,7 +2,6 @@
"Middleware for logging a query before it is processed.
(Various other middleware functions log the query as well in different stages.)"
(
:require
[
clojure.tools.logging
:as
log
]
[
medley.core
:as
m
]
[
metabase.query-processor.interface
:as
i
]
[
metabase.util
:as
u
]))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment