Skip to content

Commit 69a1344

Browse files
committed
add comments to error type
1 parent ffe2441 commit 69a1344

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

crates/bevy_mod_scripting_core/src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<C: Context, R: Runtime> Command for CreateOrUpdateScript<C, R> {
116116
match ctxt {
117117
Ok(ctxt) => contexts.insert(ctxt),
118118
Err(e) => {
119-
handle_script_errors(world, &format!("Failed to load context for script with id: {}. With runtime type: {} and context type: {}", self.id, type_name::<R>(), type_name::<C>()), [e].into_iter());
119+
handle_script_errors(world, [e.with_context(format!("Loading context for script with id: {}. With runtime type: {} and context type: {}", self.id, type_name::<R>(), type_name::<C>()))].into_iter());
120120
return;
121121
}
122122
}

crates/bevy_mod_scripting_core/src/error.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,6 @@ impl ScriptError {
119119
}))
120120
}
121121

122-
pub fn with_context<S: ToString>(self, context: S) -> Self {
123-
Self(Arc::new(ScriptErrorInner {
124-
script: self.0.script.clone(),
125-
context: context.to_string(),
126-
reason: self.0.reason.clone(),
127-
}))
128-
}
129-
130122
pub fn with_script<S: ToString>(self, script: S) -> Self {
131123
Self(Arc::new(ScriptErrorInner {
132124
script: Some(script.to_string()),
@@ -135,10 +127,10 @@ impl ScriptError {
135127
}))
136128
}
137129

138-
pub fn with_appended_context<S: ToString>(self, context: S) -> Self {
130+
pub fn with_context<S: ToString>(self, context: S) -> Self {
139131
Self(Arc::new(ScriptErrorInner {
140132
script: self.0.script.clone(),
141-
context: format!("{}. {}", self.0.context, context.to_string()),
133+
context: format!("{}\n{}", self.0.context, context.to_string()),
142134
reason: self.0.reason.clone(),
143135
}))
144136
}
@@ -149,11 +141,11 @@ impl std::fmt::Display for ScriptError {
149141
if let Some(script) = &self.0.script {
150142
write!(
151143
f,
152-
"error in script `{}`: {}.\n{}",
144+
"error in script `{}`: {}.\nContext:{}",
153145
script, self.0.reason, self.0.context
154146
)
155147
} else {
156-
write!(f, "error: {}.\n{}", self.0.reason, self.0.context)
148+
write!(f, "error: {}.\nContext:{}", self.0.reason, self.0.context)
157149
}
158150
}
159151
}
@@ -162,14 +154,14 @@ impl DisplayWithWorld for ScriptError {
162154
fn display_with_world(&self, world: crate::bindings::WorldGuard) -> String {
163155
if let Some(script) = &self.0.script {
164156
format!(
165-
"error in script `{}`: {}.\n{}",
157+
"error in script `{}`: {}.\nContext:{}",
166158
script,
167159
self.0.reason.display_with_world(world),
168160
self.0.context
169161
)
170162
} else {
171163
format!(
172-
"error: {}.\n{}",
164+
"error: {}.\nContext:{}",
173165
self.0.reason.display_with_world(world),
174166
self.0.context
175167
)
@@ -211,72 +203,93 @@ impl From<InteropError> for ScriptError {
211203
}
212204

213205
impl InteropError {
206+
/// Thrown if a callback requires world access, but is unable to do so due
207+
/// to the world not being reachable at all via any mechanism.
214208
pub fn missing_world() -> Self {
215209
Self(Arc::new(InteropErrorInner::MissingWorld))
216210
}
217211

212+
/// Thrown if a callback requires world access, but is unable to do so due
213+
/// to the world being dropped. I.e. Symptom of a script trying to persist a world reference somewhere.
218214
pub fn stale_world_access() -> Self {
219215
Self(Arc::new(InteropErrorInner::StaleWorldAccess))
220216
}
221217

218+
/// Thrown if a base type is not registered with the reflection system
219+
/// and therefore the reference cannot be dereferenced
222220
pub fn unregistered_base(base: ReflectBaseType) -> Self {
223221
Self(Arc::new(InteropErrorInner::UnregisteredBase { base }))
224222
}
225223

224+
/// Thrown if a base type is not registered with the reflection system
225+
/// with the specific type data.
226226
pub fn missing_type_data(type_id: TypeId, type_data: String) -> Self {
227227
Self(Arc::new(InteropErrorInner::MissingTypeData {
228228
type_id,
229229
type_data,
230230
}))
231231
}
232232

233+
/// Thrown if a type cannot be converted from reflect, this can happen if the type was unable to
234+
/// re-construct itself from a dynamic value.
233235
pub fn failed_from_reflect(type_id: Option<TypeId>, reason: String) -> Self {
234236
Self(Arc::new(InteropErrorInner::FailedFromReflect {
235237
type_id,
236238
reason,
237239
}))
238240
}
239241

242+
/// Thrown if access to the given reflection base is required but cannot be claimed.
243+
/// This is likely due to some other script already claiming access to the base.
240244
pub fn cannot_claim_access(base: ReflectBaseType) -> Self {
241245
Self(Arc::new(InteropErrorInner::CannotClaimAccess { base }))
242246
}
243247

248+
/// Thrown if a conversion into the given type is impossible.
249+
/// Should be thrown with context on the other type if possible.
244250
pub fn impossible_conversion(into: TypeId) -> Self {
245251
Self(Arc::new(InteropErrorInner::ImpossibleConversion { into }))
246252
}
247253

254+
/// Thrown if a value was expected to be of one type but was of another
248255
pub fn type_mismatch(expected: TypeId, got: Option<TypeId>) -> Self {
249256
Self(Arc::new(InteropErrorInner::TypeMismatch { expected, got }))
250257
}
251258

259+
/// Identical to [`InteropError::type_mismatch`] but for more abstract types
252260
pub fn string_type_mismatch(expected: String, got: Option<TypeId>) -> Self {
253261
Self(Arc::new(InteropErrorInner::StringTypeMismatch {
254262
expected,
255263
got,
256264
}))
257265
}
258266

267+
/// Thrown if a [`ScriptValue`] could not be converted to the expected type
259268
pub fn value_mismatch(expected: TypeId, got: ScriptValue) -> Self {
260269
Self(Arc::new(InteropErrorInner::ValueMismatch { expected, got }))
261270
}
262271

272+
/// Thrown if a downcast from a reflect reference to a specific type failed
263273
pub fn could_not_downcast(from: ReflectReference, to: TypeId) -> Self {
264274
Self(Arc::new(InteropErrorInner::CouldNotDowncast { from, to }))
265275
}
266276

277+
/// Thrown if a garbage collected allocation was attempted to be accessed
267278
pub fn garbage_collected_allocation(reference: ReflectReference) -> Self {
268279
Self(Arc::new(InteropErrorInner::GarbageCollectedAllocation {
269280
reference,
270281
}))
271282
}
272283

284+
/// Thrown if a reflection path is invalid
273285
pub fn reflection_path_error(error: String, reference: Option<ReflectReference>) -> Self {
274286
Self(Arc::new(InteropErrorInner::ReflectionPathError {
275287
error,
276288
reference,
277289
}))
278290
}
279291

292+
/// Thrown if an operation is not supported on the given base type, optionally with a value argument that was used to carry it out
280293
pub fn unsupported_operation(
281294
base: Option<TypeId>,
282295
value: Option<Box<dyn PartialReflect>>,
@@ -289,20 +302,24 @@ impl InteropError {
289302
}))
290303
}
291304

305+
/// Thrown if an invalid index operation was attempted on a value
292306
pub fn invalid_index(value: ScriptValue, reason: String) -> Self {
293307
Self(Arc::new(InteropErrorInner::InvalidIndex { value, reason }))
294308
}
295309

310+
/// Thrown if an entity was missing or invalid
296311
pub fn missing_entity(entity: Entity) -> Self {
297312
Self(Arc::new(InteropErrorInner::MissingEntity { entity }))
298313
}
299314

315+
/// Thrown if a component was invalid
300316
pub fn invalid_component(component_id: ComponentId) -> Self {
301317
Self(Arc::new(InteropErrorInner::InvalidComponent {
302318
component_id,
303319
}))
304320
}
305321

322+
/// Thrown when an error happens in a function call. The inner error provides details on the error.
306323
pub fn function_interop_error(
307324
function_info: &FunctionInfo,
308325
argument_info: Option<&ArgInfo>,
@@ -327,6 +344,9 @@ impl InteropError {
327344
}))
328345
}
329346

347+
/// Thrown when the error happens after a function call, and an error is thrown by bevy.
348+
///
349+
/// I.e. mismatch in args, or invalid number of arguments
330350
pub fn function_call_error(inner: FunctionError) -> Self {
331351
Self(Arc::new(InteropErrorInner::FunctionCallError { inner }))
332352
}

crates/bevy_mod_scripting_core/src/systems.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,12 @@ pub fn event_handler<L: IntoCallbackLabel, A: Args, C: Context, R: Runtime>(
187187
world,
188188
)
189189
.map_err(|e| {
190-
// println!("{e:?}");
191-
e.with_script(script.id.clone())
190+
e.with_script(script.id.clone()).with_context(&format!(
191+
"Event handling for: Runtime {}, Context: {}, Args: {}",
192+
type_name::<R>(),
193+
type_name::<C>(),
194+
type_name::<A>()
195+
))
192196
});
193197

194198
push_err_and_continue!(errors, handler_result)
@@ -199,22 +203,12 @@ pub fn event_handler<L: IntoCallbackLabel, A: Args, C: Context, R: Runtime>(
199203
world.insert_non_send_resource(runtime_container);
200204
world.insert_non_send_resource(script_contexts);
201205

202-
handle_script_errors(
203-
world,
204-
&format!(
205-
"Encountered error in event handling for: Runtime {}, Context: {}, Args: {}",
206-
type_name::<R>(),
207-
type_name::<C>(),
208-
type_name::<A>()
209-
),
210-
errors.into_iter(),
211-
);
206+
handle_script_errors(world, errors.into_iter());
212207
}
213208

214209
/// Handles errors caused by script execution and sends them to the error event channel
215210
pub(crate) fn handle_script_errors<I: Iterator<Item = ScriptError> + Clone>(
216211
world: &mut World,
217-
context: &str,
218212
errors: I,
219213
) {
220214
let mut error_events = world
@@ -227,7 +221,7 @@ pub(crate) fn handle_script_errors<I: Iterator<Item = ScriptError> + Clone>(
227221

228222
for error in errors {
229223
let arc_world = WorldGuard::new(WorldAccessGuard::new(world));
230-
bevy::log::error!("{}. {}", context, error.display_with_world(arc_world));
224+
bevy::log::error!("{}", error.display_with_world(arc_world));
231225
}
232226
}
233227

0 commit comments

Comments
 (0)