Skip to content

Commit

Permalink
avoid error when unsupported nodes, fills or other kind of elements. …
Browse files Browse the repository at this point in the history
…Improve error messages (#49)
  • Loading branch information
jordisala1991 committed Apr 19, 2024
1 parent 08473b5 commit f469fc1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
5 changes: 4 additions & 1 deletion plugin-src/transformers/partials/transformChildren.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { transformSceneNode } from '@plugin/transformers';

import { PenpotNode } from '@ui/lib/types/penpotNode';
import { Children } from '@ui/lib/types/utils/children';

export const transformChildren = async (
Expand All @@ -8,6 +9,8 @@ export const transformChildren = async (
baseY: number = 0
): Promise<Children> => {
return {
children: await Promise.all(node.children.map(child => transformSceneNode(child, baseX, baseY)))
children: (
await Promise.all(node.children.map(child => transformSceneNode(child, baseX, baseY)))
).filter((child): child is PenpotNode => !!child)
};
};
4 changes: 2 additions & 2 deletions plugin-src/transformers/transformSceneNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const transformSceneNode = async (
node: SceneNode,
baseX: number = 0,
baseY: number = 0
): Promise<PenpotNode> => {
): Promise<PenpotNode | undefined> => {
// @TODO: when penpot 2.0, manage image as fills for the basic types
if (
'fills' in node &&
Expand Down Expand Up @@ -49,5 +49,5 @@ export const transformSceneNode = async (
return transformPathNode(node, baseX, baseY);
}

throw new Error(`Unsupported node type: ${node.type}`);
console.error(`Unsupported node type: ${node.type}`);
};
2 changes: 1 addition & 1 deletion plugin-src/translators/translateFills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const translateFill = (fill: Paint, width: number, height: number): Fill
return translateGradientLinearFill(fill, width, height);
}

console.error('Color type ' + fill.type + ' not supported yet');
console.error(`Unsupported fill type: ${fill.type}`);
};

export const translateFills = (
Expand Down
4 changes: 2 additions & 2 deletions ui-src/converters/createGradientFill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Gradient, LINEAR_TYPE, RADIAL_TYPE } from '@ui/lib/types/utils/gradient';

export const createGradientFill = ({ type, ...rest }: Gradient): Gradient => {
export const createGradientFill = ({ type, ...rest }: Gradient): Gradient | undefined => {
switch (type) {
case 'linear':
return {
Expand All @@ -14,5 +14,5 @@ export const createGradientFill = ({ type, ...rest }: Gradient): Gradient => {
};
}

throw new Error(`Unsupported gradient type: ${String(type)}`);
console.error(`Unsupported gradient type: ${String(type)}`);
};
22 changes: 14 additions & 8 deletions ui-src/translators/translatePathContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import {
} from '@ui/lib/types/path/PathContent';

export const translatePathContent = (content: PathContent): PathContent =>
content.map(({ command, ...rest }) => {
return {
command: translatePathCommand(command),
...rest
} as Segment;
});
content
.map(({ command: stringCommand, ...rest }) => {
const command = translatePathCommand(stringCommand);

const translatePathCommand = (command: Command): Command => {
if (!command) return null;

return {
command,
...rest
} as Segment;
})
.filter((command): command is Segment => !!command);

const translatePathCommand = (command: Command): Command | undefined => {
switch (command) {
case 'line-to':
return VECTOR_LINE_TO;
Expand All @@ -28,5 +34,5 @@ const translatePathCommand = (command: Command): Command => {
return VECTOR_CURVE_TO;
}

throw new Error('Unknown path command');
console.error(`Unsupported svg command type: ${String(command)}`);
};

0 comments on commit f469fc1

Please sign in to comment.