forked from MapComplete/MapComplete
UX: plantnet shows better error message if no plant is detected
This commit is contained in:
parent
ea640468e8
commit
875b3a3ea8
4 changed files with 47 additions and 979 deletions
25
src/Utils.ts
25
src/Utils.ts
|
@ -879,7 +879,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
): Promise<
|
||||
| { content: string }
|
||||
| { redirect: string }
|
||||
| { error: string; url: string; statuscode?: number }
|
||||
| { error: string; url: string; statuscode?: number, errContent?: string }
|
||||
> {
|
||||
let result = undefined
|
||||
for (let i = 0; i < maxAttempts; i++) {
|
||||
|
@ -917,7 +917,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
): Promise<
|
||||
| { content: string }
|
||||
| { redirect: string }
|
||||
| { error: string; url: string; statuscode?: number }
|
||||
| { error: string; url: string; statuscode?: number, errContent: string }
|
||||
> {
|
||||
if (this.externalDownloadFunction !== undefined) {
|
||||
return this.externalDownloadFunction(url, headers)
|
||||
|
@ -931,12 +931,13 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
} else if (xhr.status === 302) {
|
||||
resolve({ redirect: xhr.getResponseHeader("location") })
|
||||
} else if (xhr.status === 509 || xhr.status === 429) {
|
||||
resolve({ error: "rate limited", url, statuscode: xhr.status })
|
||||
resolve({ error: "rate limited", url, statuscode: xhr.status, errContent: xhr.responseText })
|
||||
} else {
|
||||
resolve({
|
||||
error: "other error: " + xhr.statusText + ", " + xhr.responseText,
|
||||
url,
|
||||
statuscode: xhr.status,
|
||||
errContent: xhr.responseText
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1010,8 +1011,9 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
url: string,
|
||||
maxCacheTimeMs: number,
|
||||
headers?: Record<string, string>,
|
||||
dontCacheErrors = false
|
||||
): Promise<{ content: T } | { error: string; url: string; statuscode?: number }> {
|
||||
dontCacheErrors = false,
|
||||
maxAttempts = 3
|
||||
): Promise<{ content: T } | { error: string; url: string; statuscode?: number, errContent?: object }> {
|
||||
const cached = Utils._download_cache.get(url)
|
||||
if (cached !== undefined) {
|
||||
if (new Date().getTime() - cached.timestamp <= maxCacheTimeMs) {
|
||||
|
@ -1021,7 +1023,8 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
const promise =
|
||||
/*NO AWAIT as we work with the promise directly */ Utils.downloadJsonAdvanced<T>(
|
||||
url,
|
||||
headers
|
||||
headers,
|
||||
maxAttempts
|
||||
)
|
||||
Utils._download_cache.set(url, { promise, timestamp: new Date().getTime() })
|
||||
try {
|
||||
|
@ -1060,15 +1063,19 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
|
||||
public static async downloadJsonAdvanced<T = object | []>(
|
||||
url: string,
|
||||
headers?: Record<string, string>
|
||||
): Promise<{ content: T } | { error: string; url: string; statuscode?: number }> {
|
||||
headers?: Record<string, string>,
|
||||
maxAttempts = 3
|
||||
): Promise<{ content: T } | { error: string; url: string; statuscode?: number, errContent?: object }> {
|
||||
const injected = Utils.injectedDownloads[url]
|
||||
if (injected !== undefined) {
|
||||
return { content: injected }
|
||||
}
|
||||
const result = await Utils.downloadAdvanced(
|
||||
url,
|
||||
Utils.Merge({ accept: "application/json" }, headers ?? {})
|
||||
Utils.Merge({ accept: "application/json" }, headers ?? {}),
|
||||
"GET",
|
||||
undefined,
|
||||
maxAttempts
|
||||
)
|
||||
if (result["error"] !== undefined) {
|
||||
return <{ error: string; url: string; statuscode?: number }>result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue