<commit_before>package com.olegych.scastie
package client

import play.api.libs.json.Json

import org.scalajs.dom.window.localStorage

object LocalStorage {
  private val stateKey = "state"

  def save(state: ScastieState): Unit = {
    localStorage.setItem(stateKey, Json.stringify(Json.toJson(state)))
  }

  def load: Option[ScastieState] =
    Option(localStorage.getItem(stateKey))
      .flatMap(raw => Json.fromJson[ScastieState](Json.parse(raw)).asOpt)
}<commit_msg>Client: Catch exception when saving to local storage fix #195<commit_after>package com.olegych.scastie
package client

import play.api.libs.json.Json

import org.scalajs.dom
import org.scalajs.dom.window.localStorage

object LocalStorage {
  private val stateKey = "state"

  def save(state: ScastieState): Unit = {
    localStorage.setItem(stateKey, Json.stringify(Json.toJson(state)))
  }

  def load: Option[ScastieState] = {
    try {
      Option(localStorage.getItem(stateKey))
        .flatMap(raw => Json.fromJson[ScastieState](Json.parse(raw)).asOpt)
    } catch {
      case e: Exception => {
        dom.console.log(e.toString)
        None
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.11877729257641921, 'fraction_numerical': 0.0026200873362445414, 'mean_word_length': 3.876595744680851, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6961252', 'n_tokens_mistral': 388, 'n_tokens_neox': 380, 'n_words': 70}