Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deno: handle error sources to display full errors #2454

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions deno_webgpu/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError;
use deno_core::ResourceId;
use serde::Serialize;
use std::convert::From;
use std::error::Error;
use std::fmt;
use wgpu_core::binding_model::CreateBindGroupError;
use wgpu_core::binding_model::CreateBindGroupLayoutError;
Expand All @@ -29,6 +30,18 @@ use wgpu_core::resource::CreateSamplerError;
use wgpu_core::resource::CreateTextureError;
use wgpu_core::resource::CreateTextureViewError;

fn fmt_err(err: &(dyn Error + 'static)) -> String {
let mut output = String::from(err.to_string());

let mut e = err.source();
while let Some(source) = e {
output.push_str(&format!(": {source}"));
e = source.source();
}

output
}

#[derive(Serialize)]
pub struct WebGpuResult {
pub rid: Option<ResourceId>,
Expand Down Expand Up @@ -79,7 +92,7 @@ impl From<CreateBufferError> for WebGpuError {
match err {
CreateBufferError::Device(err) => err.into(),
CreateBufferError::AccessError(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -89,7 +102,7 @@ impl From<DeviceError> for WebGpuError {
match err {
DeviceError::Lost => WebGpuError::Lost,
DeviceError::OutOfMemory => WebGpuError::OutOfMemory,
DeviceError::Invalid => WebGpuError::Validation(err.to_string()),
DeviceError::Invalid => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -98,7 +111,7 @@ impl From<BufferAccessError> for WebGpuError {
fn from(err: BufferAccessError) -> Self {
match err {
BufferAccessError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -107,7 +120,7 @@ impl From<CreateBindGroupLayoutError> for WebGpuError {
fn from(err: CreateBindGroupLayoutError) -> Self {
match err {
CreateBindGroupLayoutError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -116,7 +129,7 @@ impl From<CreatePipelineLayoutError> for WebGpuError {
fn from(err: CreatePipelineLayoutError) -> Self {
match err {
CreatePipelineLayoutError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -125,82 +138,82 @@ impl From<CreateBindGroupError> for WebGpuError {
fn from(err: CreateBindGroupError) -> Self {
match err {
CreateBindGroupError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}

impl From<RenderBundleError> for WebGpuError {
fn from(err: RenderBundleError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<CreateRenderBundleError> for WebGpuError {
fn from(err: CreateRenderBundleError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<CopyError> for WebGpuError {
fn from(err: CopyError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<CommandEncoderError> for WebGpuError {
fn from(err: CommandEncoderError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<QueryError> for WebGpuError {
fn from(err: QueryError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<ComputePassError> for WebGpuError {
fn from(err: ComputePassError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<CreateComputePipelineError> for WebGpuError {
fn from(err: CreateComputePipelineError) -> Self {
match err {
CreateComputePipelineError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}

impl From<GetBindGroupLayoutError> for WebGpuError {
fn from(err: GetBindGroupLayoutError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<CreateRenderPipelineError> for WebGpuError {
fn from(err: CreateRenderPipelineError) -> Self {
match err {
CreateRenderPipelineError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}

impl From<RenderPassError> for WebGpuError {
fn from(err: RenderPassError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<CreateSamplerError> for WebGpuError {
fn from(err: CreateSamplerError) -> Self {
match err {
CreateSamplerError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -209,7 +222,7 @@ impl From<CreateShaderModuleError> for WebGpuError {
fn from(err: CreateShaderModuleError) -> Self {
match err {
CreateShaderModuleError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -218,22 +231,22 @@ impl From<CreateTextureError> for WebGpuError {
fn from(err: CreateTextureError) -> Self {
match err {
CreateTextureError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}

impl From<CreateTextureViewError> for WebGpuError {
fn from(err: CreateTextureViewError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

impl From<CreateQuerySetError> for WebGpuError {
fn from(err: CreateQuerySetError) -> Self {
match err {
CreateQuerySetError::Device(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -242,7 +255,7 @@ impl From<QueueSubmitError> for WebGpuError {
fn from(err: QueueSubmitError) -> Self {
match err {
QueueSubmitError::Queue(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}
Expand All @@ -251,14 +264,14 @@ impl From<QueueWriteError> for WebGpuError {
fn from(err: QueueWriteError) -> Self {
match err {
QueueWriteError::Queue(err) => err.into(),
err => WebGpuError::Validation(err.to_string()),
err => WebGpuError::Validation(fmt_err(&err)),
}
}
}

impl From<ClearError> for WebGpuError {
fn from(err: ClearError) -> Self {
WebGpuError::Validation(err.to_string())
WebGpuError::Validation(fmt_err(&err))
}
}

Expand Down