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) { // regular Error Object 37 return String(errorObject.message); 38 } else if (errorObject.error?.message) { // API result 39 return String(errorObject.error.message); 40 } 41 } 42 const asString = String(e); 43 if (asString === '[object Object]') { 44 try { 45 return JSON.stringify(e); 46 } catch (stringifyError) { 47 // ignore failures and just fall through 48 } 49 } 50 return asString; 51} 52 53// Occasionally operations using the cache API throw: 54// 'UnknownError: Unexpected internal error. {}' 55// It's not clear under which circumstances this can occur. A dive of 56// the Chromium code didn't shed much light: 57// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/cache_storage/cache_storage_error.cc;l=26;drc=4cfe86482b000e848009077783ba35f83f3c3cfe 58// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/cache_storage/cache_storage_cache.cc;l=1686;drc=ab68c05beb790d04d1cb7fd8faa0a197fb40d399 59// Given the error is not actionable at present and caching is 'best 60// effort' in any case ignore this error. We will want to throw for 61// errors in general though so as not to hide errors we actually could 62// fix. 63// See b/227785665 for an example. 64export function ignoreCacheUnactionableErrors<T>(e: unknown, result: T): T { 65 if (getErrorMessage(e).includes('UnknownError')) { 66 return result; 67 } else { 68 throw e; 69 } 70} 71