Skip to content

Commit

Permalink
Handle error when pass wrong uri to SvgFromUri component (#2289)
Browse files Browse the repository at this point in the history
# Summary

We want to add the ability to handle the Error when someone passes the
wrong URI to the `SvgFromUri` component.

Closes #2148 

## Test Plan

Manual tests in TestsExample app. (Test2148)
  • Loading branch information
bohdanprog authored Jun 6, 2024
1 parent 1703399 commit 8733bea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
7 changes: 4 additions & 3 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
import React from 'react';

import ColorTest from './src/ColorTest';
import PointerEventsBoxNone from './src/PointerEventsBoxNone';
import Test1374 from './src/Test1374';
import Test1718 from './src/Test1718';
import Test1813 from './src/Test1813';
import Test1845 from './src/Test1845';
import Test2080 from './src/Test2080';
import PointerEventsBoxNone from './src/PointerEventsBoxNone';
import Test1986 from './src/Test1986';
import Test2071 from './src/Test2071';
import Test2080 from './src/Test2080';
import Test2089 from './src/Test2089';
import Test2148 from './src/Test2148';
import Test2196 from './src/Test2196';
import Test2266 from './src/Test2266';
import Test1986 from './src/Test1986';
import Test2276 from './src/Test2276';

export default function App() {
Expand Down
20 changes: 20 additions & 0 deletions TestsExample/src/Test2148.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, {useState} from 'react';
import {Text, View} from 'react-native';
import {SvgFromUri} from 'react-native-svg';

export default function App() {
const [error, setError] = useState<Error>();
return (
<View style={{flex: 1, backgroundColor: 'red', padding: 50}}>
<SvgFromUri
uri={
'https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.svgrepo.com%2Fsvg%2F5154%2Fhand-pointer&psig=AOvVaw2e3ud_ho3oPjy1VSLw0nch&ust=1717666569513000&source=images&cd=vfe&opi=89978449&ved=0CBIQjRxqFwoTCLixjuCUxIYDFQAAAAAdAAAAABAY'
}
width={24}
height={24}
onError={setError}
/>
<Text style={{marginTop: 50, color: '#fff'}}>{error?.message}</Text>
</View>
);
}
9 changes: 7 additions & 2 deletions src/xml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ export class SvgFromXml extends Component<XmlProps, XmlState> {
}

parse(xml: string | null) {
const { onError = err } = this.props;
try {
this.setState({ ast: xml ? parse(xml) : null });
} catch (e) {
console.error(e);
const error = e as Error;
onError({
...error,
message: `[RNSVG] Couldn't parse SVG, reason: ${error.message}`,
});
}
}

Expand Down Expand Up @@ -213,7 +218,7 @@ export class SvgFromUri extends Component<UriProps, UriState> {
props,
state: { xml },
} = this;
return <SvgFromXml xml={xml} override={props} />;
return <SvgFromXml xml={xml} override={props} onError={props.onError} />;
}
}

Expand Down

0 comments on commit 8733bea

Please sign in to comment.