1// Copyright (C) 2022 The Android Open Source Project 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// Errors in JavaScript are (sadly) extremely free form. Three common 16// cases are: 17// - the error is a string 18// - the error object itself has a 'message' field (normally because 19// its an instance of Error or a subclass of Error). 20// - the outer object wraps an error under the 'error' key. As in: 21// https://google.github.io/styleguide/jsoncstyleguide.xml#Reserved_Property_Names_in_the_error_object 22// TODO(hjd): Can this last case actually occur for us? Maybe this 23// too closely followed the code from flash station? 24interface ErrorLikeObject { 25 message?: unknown; 26 error?: {message?: unknown}; 27 stack?: unknown; 28 code?: unknown; 29} 30 31// Attempt to coerce an error object into a string message. 32// Sometimes an error message is wrapped in an Error object, sometimes not. 33export function getErrorMessage(e: unknown | undefined | null) { 34 if (e && typeof e === 'object') { 35 const errorObject = e as ErrorLikeObject; 36 if (errorObject.message) { 37 // regular Error Object 38 return String(errorObject.message); 39 } else if (errorObject.error?.message) { 40 // API result 41 return String(errorObject.error.message); 42 } 43 } 44 const asString = String(e); 45 if (asString === '[object Object]') { 46 try { 47 return JSON.stringify(e); 48 } catch (stringifyError) { 49 // ignore failures and just fall through 50 } 51 } 52 return asString; 53} 54 55// Occasionally operations using the cache API throw: 56// 'UnknownError: Unexpected internal error. {}' 57// It's not clear under which circumstances this can occur. A dive of 58// the Chromium code didn't shed much light: 59// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/cache_storage/cache_storage_error.cc;l=26;drc=4cfe86482b000e848009077783ba35f83f3c3cfe 60// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/cache_storage/cache_storage_cache.cc;l=1686;drc=ab68c05beb790d04d1cb7fd8faa0a197fb40d399 61// Given the error is not actionable at present and caching is 'best 62// effort' in any case ignore this error. We will want to throw for 63// errors in general though so as not to hide errors we actually could 64// fix. 65// See b/227785665 for an example. 66export function ignoreCacheUnactionableErrors<T>(e: unknown, result: T): T { 67 if (getErrorMessage(e).includes('UnknownError')) { 68 return result; 69 } else { 70 throw e; 71 } 72} 73