fyrox_graphics/error.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Contains all possible errors that may occur during rendering, initialization of
22//! renderer structures, or GAPI.
23
24use std::{
25 error::Error,
26 ffi::NulError,
27 fmt::{Display, Formatter},
28};
29
30/// Set of possible renderer errors.
31#[derive(Debug)]
32pub enum FrameworkError {
33 /// Compilation of a shader has failed.
34 ShaderCompilationFailed {
35 /// Name of shader.
36 shader_name: String,
37 /// Compilation error message.
38 error_message: String,
39 },
40 /// Means that shader link stage failed, exact reason is inside `error_message`
41 ShaderLinkingFailed {
42 /// Name of shader.
43 shader_name: String,
44 /// Linking error message.
45 error_message: String,
46 },
47 /// Shader source contains invalid characters.
48 FaultyShaderSource,
49 /// There is no such shader uniform (could be optimized out).
50 UnableToFindShaderUniform(String),
51 /// There is no such shader uniform block.
52 UnableToFindShaderUniformBlock(String),
53 /// Texture has invalid data - insufficient size.
54 InvalidTextureData {
55 /// Expected data size in bytes.
56 expected_data_size: usize,
57 /// Actual data size in bytes.
58 actual_data_size: usize,
59 },
60 /// None variant was passed as texture data, but engine does not support it.
61 EmptyTextureData,
62 /// Means that you tried to draw element range from GeometryBuffer that
63 /// does not have enough elements.
64 InvalidElementRange {
65 /// First index.
66 start: usize,
67 /// Last index.
68 end: usize,
69 /// Total amount of triangles.
70 total: usize,
71 },
72 /// Means that attribute descriptor tries to define an attribute that does
73 /// not exists in vertex, or it does not match size. For example you have vertex:
74 /// pos: float2,
75 /// normal: float3
76 /// But you described second attribute as Float4, then you'll get this error.
77 InvalidAttributeDescriptor,
78 /// Framebuffer is invalid.
79 InvalidFrameBuffer,
80 /// OpenGL failed to construct framebuffer.
81 FailedToConstructFBO,
82 /// Custom error. Usually used for internal errors.
83 Custom(String),
84 /// Graphics server disconnected.
85 GraphicsServerUnavailable,
86}
87
88impl std::error::Error for FrameworkError {}
89
90impl Display for FrameworkError {
91 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
92 match self {
93 FrameworkError::ShaderCompilationFailed {
94 shader_name,
95 error_message,
96 } => {
97 write!(
98 f,
99 "Compilation of \"{shader_name}\" shader has failed: {error_message}",
100 )
101 }
102 FrameworkError::ShaderLinkingFailed {
103 shader_name,
104 error_message,
105 } => {
106 write!(
107 f,
108 "Linking shader \"{shader_name}\" failed: {error_message}",
109 )
110 }
111 FrameworkError::FaultyShaderSource => {
112 write!(f, "Shader source contains invalid characters")
113 }
114 FrameworkError::UnableToFindShaderUniform(v) => {
115 write!(f, "There is no such shader uniform: {v}")
116 }
117 FrameworkError::UnableToFindShaderUniformBlock(v) => {
118 write!(f, "There is no such shader uniform block: {v}")
119 }
120 FrameworkError::InvalidTextureData {
121 expected_data_size,
122 actual_data_size,
123 } => {
124 write!(
125 f,
126 "Texture has invalid data (insufficent size): \
127 expected {expected_data_size}, actual: {actual_data_size}",
128 )
129 }
130 FrameworkError::EmptyTextureData => {
131 write!(
132 f,
133 "None variant was passed as texture data, but engine does not support it."
134 )
135 }
136 FrameworkError::InvalidElementRange { start, end, total } => {
137 write!(
138 f,
139 "Tried to draw element from GeometryBuffer that does not have enough \
140 elements: start: {start}, end: {end}, total: {total}",
141 )
142 }
143 FrameworkError::InvalidAttributeDescriptor => {
144 write!(
145 f,
146 "An attribute descriptor tried to define an attribute that \
147 does not exist in vertex or doesn't match size."
148 )
149 }
150 FrameworkError::InvalidFrameBuffer => {
151 write!(f, "Framebuffer is invalid")
152 }
153 FrameworkError::FailedToConstructFBO => {
154 write!(f, "OpenGL failed to construct framebuffer.")
155 }
156 FrameworkError::Custom(v) => {
157 write!(f, "Custom error: {v}")
158 }
159 FrameworkError::GraphicsServerUnavailable => {
160 write!(f, "Graphics server disconnected.")
161 }
162 }
163 }
164}
165
166impl From<NulError> for FrameworkError {
167 fn from(_: NulError) -> Self {
168 Self::FaultyShaderSource
169 }
170}
171
172impl From<String> for FrameworkError {
173 fn from(v: String) -> Self {
174 Self::Custom(v)
175 }
176}
177
178impl From<Box<dyn Error>> for FrameworkError {
179 fn from(e: Box<dyn Error>) -> Self {
180 Self::Custom(format!("{e:?}"))
181 }
182}