Skip to content

Commit

Permalink
Added property to MagickImage that can be used to retrieve warning ev…
Browse files Browse the repository at this point in the history
…ents.
  • Loading branch information
dlemstra committed Nov 23, 2023
1 parent 3459f4f commit a95093e
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 85 deletions.
115 changes: 115 additions & 0 deletions src/enums/magick-error-severity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,121 @@
* Specifies the severity of a MagickError.
*/
export enum MagickErrorSeverity {
/**
* Warning.
*/
Warning = 300,

/**
* Resource limit warning.
*/
ResourceLimitWarning = 300,

/**
* Type warning.
*/
TypeWarning = 305,

/**
* Option warning.
*/
OptionWarning = 310,

/**
* Delegate warning.
*/
DelegateWarning = 315,

/**
* Missing delegate warning.
*/
MissingDelegateWarning = 320,

/**
* Corrupt image warning.
*/
CorruptImageWarning = 325,

/**
* File open warning.
*/
FileOpenWarning = 330,

/**
* Blob warning.
*/
BlobWarning = 335,

/**
* Stream warning.
*/
StreamWarning = 340,

/**
* Cache warning.
*/
CacheWarning = 345,

/**
* Coder warning.
*/
CoderWarning = 350,

/**
* Filter warning.
*/
FilterWarning = 352,

/**
* Module warning.
*/
ModuleWarning = 355,

/**
* Draw warning.
*/
DrawWarning = 360,

/**
* Image warning.
*/
ImageWarning = 365,

/**
* Wand warning.
*/
WandWarning = 370,

/**
* Random warning.
*/
RandomWarning = 375,

/**
* X server warning.
*/
XServerWarning = 380,

/**
* Monitor warning.
*/
MonitorWarning = 385,

/**
* Registry warning.
*/
RegistryWarning = 390,

/**
* Configure warning.
*/
ConfigureWarning = 395,

/**
* Policy warning.
*/
PolicyWarning = 399,

/**
* Error.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/events/warning-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

import { MagickError } from "../magick-error";

/**
* Class for warning events.
*/
export class WarningEvent {
/** @internal */
constructor(error: MagickError) {
this.error = error;
}

/**
* Gets the warning that was raised.
*/
readonly error: MagickError;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export * from './enums/text-decoration';
export * from './enums/virtual-pixel-method';
export * from './events/log-event';
export * from './events/progress-event';
export * from './events/warning-event';
export * from './formats/dng/dng-interpolation';
export * from './formats/dng/dng-output-color';
export * from './formats/dng/dng-read-defines';
Expand Down
16 changes: 10 additions & 6 deletions src/internal/exception/exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,35 @@ export class Exception {
return success();
}

static usePointer<TReturnType>(func: (exception: number) => TReturnType): TReturnType {
static usePointer<TReturnType>(func: (exception: number) => TReturnType, onWarning?: (error: MagickError) => void): TReturnType {
return Pointer.use(pointer => {
const result = func(pointer.ptr);

return Exception.checkException(pointer, result);
return Exception.checkException(pointer, result, onWarning);
});
}

static use<TReturnType>(func: (exception: Exception) => TReturnType): TReturnType {
static use<TReturnType>(func: (exception: Exception) => TReturnType, onWarning?: (error: MagickError) => void): TReturnType {
return Pointer.use(pointer => {
const result = func(new Exception(pointer));

return Exception.checkException(pointer, result);
return Exception.checkException(pointer, result, onWarning);
});
}

private static checkException<TReturnType>(exception: Pointer, result: TReturnType): TReturnType {
private static checkException<TReturnType>(exception: Pointer, result: TReturnType, onWarning?: (error: MagickError) => void): TReturnType {
if (!Exception.isRaised(exception))
return result;

const severity = Exception.getErrorSeverity(exception.value);
if (severity >= MagickErrorSeverity.Error)
Exception.throw(exception, severity);
else
else if (onWarning !== undefined) {
const error = Exception.createError(exception.value, severity);
onWarning(error);
} else {
Exception.dispose(exception);
}

return result;
}
Expand Down
Loading

0 comments on commit a95093e

Please sign in to comment.