Skip to content

Commit b5e1013

Browse files
authored
Merge pull request #4541 from alfonso-salces/MOBILE-4862
MOBILE-4862 opener.ts: Handle error when open file
2 parents 0b4448d + 2eac420 commit b5e1013

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
export const enum CoreNativeCordovaPluginResultStatus {
16+
NO_RESULT,
17+
OK,
18+
CLASS_NOT_FOUND_EXCEPTION,
19+
ILLEGAL_ACCESS_EXCEPTION,
20+
INSTANTIATION_EXCEPTION,
21+
MALFORMED_URL_EXCEPTION,
22+
IO_EXCEPTION,
23+
INVALID_ACTION,
24+
JSON_EXCEPTION,
25+
ERROR
26+
}

src/core/singletons/opener.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { CoreConfig } from '@services/config';
3030
import { CoreEvents } from '@singletons/events';
3131
import { CoreColors } from './colors';
3232
import { CorePrompts } from '@services/overlays/prompts';
33+
import { CoreNativeCordovaPluginResultStatus } from '@features/native/constants';
3334

3435
/**
3536
* Singleton with helper functions to handler open files and urls.
@@ -124,18 +125,27 @@ export class CoreOpener {
124125
// Error, use the original path.
125126
}
126127

127-
const openFile = async (path: string, mimetype?: string) => {
128+
const openFile = async (path: string, mimetype?: string, hasFailed?: boolean) => {
128129
try {
129130
if (CoreOpener.shouldOpenWithDialog(options)) {
130131
await FileOpener.showOpenWithDialog(path, mimetype || '');
131132
} else {
132133
await FileOpener.open(path, mimetype || '');
133134
}
134135
} catch (error) {
136+
if (
137+
hasFailed ||
138+
error.status !== CoreNativeCordovaPluginResultStatus.ERROR ||
139+
error.message.includes('Activity not found')
140+
) {
141+
throw error;
142+
}
143+
135144
// If the file contains the % character without encoding the open can fail. Try again encoding it.
136145
const encodedPath = encodeURI(path);
146+
137147
if (path !== encodedPath) {
138-
return await openFile(encodedPath, mimetype);
148+
return await openFile(encodedPath, mimetype, true);
139149
}
140150

141151
throw error;

0 commit comments

Comments
 (0)