{"record":{"id":"f8eec0859cd196d2","repo":"CherryHQ/cherry-studio","slug":"failed-to-clear-oauth-storage-error-instanceof","errorCode":null,"errorMessage":"Failed to clear OAuth storage: ${error instanceof Error ? error.message : String(error)}","messagePattern":"Failed to clear OAuth storage: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/main/ai/mcp/oauth/storage.ts","lineNumber":132,"sourceCode":"    return data.authServerUrl\n  }\n\n  async saveAuthServerUrl(url: string | undefined): Promise<void> {\n    const data = await this.readStorage()\n    await this.writeStorage({\n      ...data,\n      authServerUrl: url\n    })\n  }\n\n  async clear(): Promise<void> {\n    try {\n      await fs.unlink(this.filePath)\n      this.cache = null\n    } catch (error) {\n      if (error instanceof Error && 'code' in error && error.code !== 'ENOENT') {\n        logger.error('Error clearing OAuth storage:', error as Error)\n        throw new Error(`Failed to clear OAuth storage: ${error instanceof Error ? error.message : String(error)}`)\n      }\n    }\n  }\n}\n","sourceCodeStart":114,"sourceCodeEnd":137,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/oauth/storage.ts#L114-L137","documentation":"Thrown by `clear()` when deleting the OAuth storage file fails with an error other than ENOENT (file-not-found is silently ignored as a no-op since the goal — file absence — is already achieved). Any other unlink error (permissions, disk I/O, file locked) is wrapped and thrown.","triggerScenarios":"Calling `clear()` to remove the OAuth JSON file, but `fs.unlink()` fails with EACCES (permission denied), EBUSY (file locked by another process), or a disk I/O error. ENOENT is the only error code silently swallowed.","commonSituations":"Antivirus or backup software locking the file on Windows; file permissions were changed after creation; the file is on a network mount with intermittent connectivity; the process doesn't have delete permission on the directory.","solutions":["Close other applications that might be locking the file (antivirus, backup, file watchers)","Check file and directory permissions — the process needs delete permission","Retry the clear operation — transient locks often release","If persistent, manually delete the `{hash}_oauth.json` file from the config directory","On Windows, use Process Explorer to identify which process holds the file handle"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await oauthStorage.clear()\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Failed to clear OAuth storage')) {\n    // Non-fatal — file deletion is best-effort cleanup\n    logger.warn('Could not delete OAuth storage file', e)\n    return\n  }\n  throw e\n}","preventionTips":["Treat clear failures as non-fatal — the goal (removing OAuth state) can often be achieved by overwriting the file instead","Retry clear after a short delay if the file is locked by another process","On Windows, be aware of antivirus/file watcher locks that can temporarily block deletion"],"tags":["mcp","oauth","storage","file-io","cleanup"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}