<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Johann Pardanaud on Medium]]></title>
        <description><![CDATA[Stories by Johann Pardanaud on Medium]]></description>
        <link>https://medium.com/@johann.pardanaud?source=rss-ad0fadfaa0dc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KiJwy8sCeXUJnCTBnipItA.jpeg</url>
            <title>Stories by Johann Pardanaud on Medium</title>
            <link>https://medium.com/@johann.pardanaud?source=rss-ad0fadfaa0dc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 18:11:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@johann.pardanaud/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ship an Android app with a pre-populated database]]></title>
            <link>https://medium.com/@johann.pardanaud/ship-an-android-app-with-a-pre-populated-database-cd2b3aa3311f?source=rss-ad0fadfaa0dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd2b3aa3311f</guid>
            <category><![CDATA[sqlite]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[room]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Johann Pardanaud]]></dc:creator>
            <pubDate>Tue, 26 Jun 2018 14:59:51 GMT</pubDate>
            <atom:updated>2020-06-30T12:13:12.393Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fAoFhw4COO_M_2lgfZ6suw.png" /><figcaption>Icons by <a href="https://fontawesome.com/license">Font Awesome</a></figcaption></figure><p><strong>My new articles are now published on my personal website. Find an updated version of this story at </strong><a href="https://johann.pardanaud.com/blog/ship-an-android-app-with-a-pre-populated-database"><strong>https://johann.pardanaud.com/blog/ship-an-android-app-with-a-pre-populated-database</strong></a><strong>.</strong></p><p>I recently had to create an Android app with a pre-populated database containing more than 8,000 rows. Unfortunately, Android doesn&#39;t provide any API to do this easily. After searching for a few minutes, I found a blog post explaining <a href="https://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/">how to install a SQLite database from your assets to your app directory (by Juan-Manuel Fluxà)</a> , which helped me to create my own database helper.</p><p>However, this article is a bit outdated (it was written in 2009), the code could benefit from some optimizations, it doesn&#39;t handle database updates and it is written in Java (while I&#39;m in love with Kotlin ❤️).</p><h3>Choosing between Room or pure SQLite</h3><p>Android highly suggests to use their <a href="https://developer.android.com/topic/libraries/architecture/room">Room</a> library, an abstraction tool running over SQLite. It&#39;s a great API, however, it manages your data with an obscure structure. Shipping a compatible database is really complicated (and honestly, I did not manage to do it), and <a href="https://issuetracker.google.com/issues/62185732">the Android team has been asked to provide a solution</a>. Because of this complexity, we will stick to a basic SQLite database.</p><p>If using Room is a requirement for you and you don&#39;t have that much data, <a href="https://android.jlelse.eu/pre-populate-room-database-6920f9acc870">you can try what is explained in this article (by Gonzalo Martin</a>). But you should probably avoid this if you have hundreds of rows to store.</p><h3>Building your database file</h3><p>Databases in Android are using the SQLite3 file format, the extension changes nothing.</p><h4>Supporting the right version</h4><p>Be careful to use features supported by old SQLite versions, because Android runs different ones depending on the API level. You can check which versions are available <a href="https://developer.android.com/reference/android/database/sqlite/package-summary">on the <strong>android.database.sqlite</strong> package reference</a>.</p><p>For example, if you want to support Android API 21+, you shouldn&#39;t use SQLite features added after the 3.8 version.</p><h4>Metadata is optional</h4><p>While reading or writing your database, Android will use the android_metadata table to know the locale. It will be automatically added if it doesn&#39;t already exist, however you can add this table by yourself if you want to set a specific locale for your content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ec9278cb95a7f65fb83841857295d9f/href">https://medium.com/media/4ec9278cb95a7f65fb83841857295d9f/href</a></iframe><h4>Cursor adapters need a special field</h4><p>If you want to support cursor adapters, you must add a primary key field named _ID to your tables. Here&#39;s a quote <a href="https://developer.android.com/training/data-storage/sqlite#DefineContract">from the documentation</a>:</p><blockquote><strong>Note:</strong> By implementing the <a href="https://developer.android.com/reference/android/provider/BaseColumns.html">BaseColumns</a> interface, your inner class can inherit a primary key field called _ID that some Android classes such as <a href="https://developer.android.com/reference/android/widget/CursorAdapter.html">CursorAdapter</a> expect it to have. It&#39;s not required, but this can help your database work harmoniously with the Android framework.</blockquote><h3>Add the database to your project</h3><p>To ship your database with your app, you will have to add it to your assets. If you don&#39;t already have one, create the <strong>assets</strong> directory: right click on your project in the sidebar, then click on <em>New &gt;</em> <em>Folder &gt;</em> <em>Assets Folder</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qRqDw4qUXNylxAB8J0x9WQ.png" /></figure><p>Now right click on the new assets directory, then click on <em>New</em> &gt; <em>Directory</em>, and name it “databases”.</p><p>Once the directory is created, copy your database file in it and name it “mydb.sqlite3” (feel free to change this).</p><h3>Writing the database helper</h3><p>Now that your database is created and stored in the assets, we can write the logic to copy it from the assets directory to the final directory where databases are stored to be used by your app.</p><h4>The basic structure of our helper</h4><p>First, create a simple database helper just like explained <a href="https://developer.android.com/training/data-storage/sqlite">in Android&#39;s documentation</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31ccaa4115bb56965aa199954331c670/href">https://medium.com/media/31ccaa4115bb56965aa199954331c670/href</a></iframe><p>Since the database structure is already defined by the SQLite file stored in the assets, there is no need to define any logic in the onCreate and onUpgrade methods, but we must override them because they are defined as abstract.</p><h4>Installing the database from the assets</h4><p>Now we need to copy our database from the assets to the final directory, here&#39;s how:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e147597318eed049e40df3d99895a2d/href">https://medium.com/media/2e147597318eed049e40df3d99895a2d/href</a></iframe><p>The installDatabaseFromAssets method:</p><ul><li>opens an input stream for the database file in the assets (note the usage of the ASSETS_PATH constant);</li><li>opens an output stream to a file where the active databases of your app are located;</li><li>copies the data form the input stream to the output one;</li><li>closes the streams.</li></ul><p><strong>Note:</strong> For readability, I&#39;m commenting out the sections we have already treated and will provide the full code at the end of the article.</p><h4>Checking the database status</h4><p>Before installing/updating our database, we must check if it is already installed and up to date. This can be done through <a href="https://developer.android.com/training/data-storage/shared-preferences">SharedPreferences</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e25b454e8e771faf820b9555f51c1351/href">https://medium.com/media/e25b454e8e771faf820b9555f51c1351/href</a></iframe><p>On initialization, we create/open a new shared preference file to store the installed database version. We use the package name to generate a unique name, which is <a href="https://developer.android.com/training/data-storage/shared-preferences#GetSharedPreferences">recommended by Android&#39;s documentation</a>:</p><blockquote>When naming your shared preference files, you should use a name that’s uniquely identifiable to your app. An easy way to do this is prefix the file name with your <a href="https://developer.android.com/studio/build/application-id.html">application ID</a>. For example: &quot;com.example.myapp.PREFERENCE_FILE_KEY&quot;</blockquote><p>We have created two methods to check or update the database version:</p><ul><li>installedDatabaseIsOutdated compares the version stored in the shared preferences and the version written in the companion object.</li><li>writeDatabaseVersionInPreferences saves the version written in the companion object to the shared preferences.</li></ul><h4>Installing the database automatically</h4><p>You may have noticed that all our custom methods are private. This is because we don&#39;t wan&#39;t to mess with them when instanciating our database helper, we just want them to be executed when necessary.</p><p>The perfect moment to run our install/update workflow is when you try to read your database. If you worry about performance, <a href="https://developer.android.com/training/data-storage/sqlite#DbHelper">remember this from Android&#39;s documentation</a>:</p><blockquote>Because they can be long-running, be sure that you call <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase()">getWritableDatabase()</a> or <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase()">getReadableDatabase()</a> in a background thread, such as with <a href="https://developer.android.com/reference/android/os/AsyncTask.html">AsyncTask</a> or <a href="https://developer.android.com/reference/android/app/IntentService.html">IntentService</a>.</blockquote><p>Since you will have to use these two methods in a background thread anyway, adding a bit of workload to the getReadableDatabase method shouldn&#39;t be an issue:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67b5ca26f4cde80e76f20b1069336d91/href">https://medium.com/media/67b5ca26f4cde80e76f20b1069336d91/href</a></iframe><p>To install/update our database on read, we must override getReadableDatabase method to run installOrUpdateIfNecessary before returning the database. If our database is outdated (or not installed) we:</p><ul><li>remove the current database with the same name;</li><li>install the latest database version;</li><li>write the new version in the shared preferences.</li></ul><p><strong>Note:</strong> The installOrUpdateIfNecessary method has the Synchronized annotation to avoid potential issues if you call getReadableDatabase simultaneously in two different threads.</p><p>And what about the getWritableDatabase method? Well, if you&#39;re planning to update your database sometimes like me, you cannot allow the database to be writable because it would be extremely complicated to merge user modifications to your own database.</p><p>My suggestion here is to store the data generated by the user in a new database created programmatically on the device. Actually, it&#39;s great because you will be able to use <a href="https://developer.android.com/topic/libraries/architecture/room">Room</a> for this database, since you&#39;re not importing it from the assets.</p><p>If you really need to write data in your imported database, use the installOrUpdateIfNecessary method in getWritableDatabase, just like in getReadableDatabase. But remember to <em>never</em> increase the database version in the future or the user modifications will be overriden.</p><h4>Getting everything together</h4><p>Finally, here&#39;s the whole code in one piece with a (very) simple use case:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38c5596e39d42575b0667ff9b983def9/href">https://medium.com/media/38c5596e39d42575b0667ff9b983def9/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd2b3aa3311f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mapping objects in JavaScript]]></title>
            <link>https://medium.com/@johann.pardanaud/mapping-objects-in-javascript-5d660f2442cd?source=rss-ad0fadfaa0dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d660f2442cd</guid>
            <category><![CDATA[mapping]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[objects]]></category>
            <category><![CDATA[es2015]]></category>
            <dc:creator><![CDATA[Johann Pardanaud]]></dc:creator>
            <pubDate>Fri, 16 Mar 2018 15:06:11 GMT</pubDate>
            <atom:updated>2020-06-30T12:12:44.623Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ONcUWaAmMwKWMomdmNIQCQ.png" /><figcaption>Icons by <a href="https://fontawesome.com/license">Font Awesome</a></figcaption></figure><p><strong>My new articles are now published on my personal website. Find an updated version of this story at </strong><a href="https://johann.pardanaud.com/blog/mapping-objects-in-javascript"><strong>https://johann.pardanaud.com/blog/mapping-objects-in-javascript</strong></a><strong>.</strong></p><p>JavaScript has been evolving fast these last years, but object mapping is still a tricky part for beginners, especially if you don&#39;t want to mutate the original object.</p><p>Here are some code examples for ES5, ES2015 and ES2017+. They all use the map and reduce methods available on arrays. These two methods allow to separate the mapping logic from the object construction.</p><p>For each example, the goal is to transform this object:</p><pre>{<br>    a: 1,<br>    b: 2,<br>}</pre><p>To this one:</p><pre>{<br>    _a: 2,<br>    _b: 3,<br>}</pre><h4>ES5</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba009b66bfea1a23e00c584b44b3b004/href">https://medium.com/media/ba009b66bfea1a23e00c584b44b3b004/href</a></iframe><h4>ES2015 (ES6)</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fdceaed5b0c5dd31cb0d6e67beacfb8/href">https://medium.com/media/2fdceaed5b0c5dd31cb0d6e67beacfb8/href</a></iframe><h4>ES2017+</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8e08690db589c93d209d8ea2f689ffd/href">https://medium.com/media/c8e08690db589c93d209d8ea2f689ffd/href</a></iframe><h4>In the future</h4><p><a href="https://github.com/bakkot/object-from-entries">A new stage-0 proposal</a> has been presented to add Object.fromEntries, this could provide us a new way to write the last example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9f89326311ec8c37926f26609ec076d/href">https://medium.com/media/e9f89326311ec8c37926f26609ec076d/href</a></iframe><p>And it would be even better with <a href="https://github.com/tc39/proposal-pipeline-operator">the pipeline operator (stage-1)</a> to keep the method chaining:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4c0f3d8770ea3bf9fbbcbd82c30d610/href">https://medium.com/media/d4c0f3d8770ea3bf9fbbcbd82c30d610/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d660f2442cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[About circular references in PHP]]></title>
            <link>https://medium.com/@johann.pardanaud/about-circular-references-in-php-10f71f811e9?source=rss-ad0fadfaa0dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/10f71f811e9</guid>
            <category><![CDATA[reference]]></category>
            <category><![CDATA[variables]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[memories]]></category>
            <category><![CDATA[circular-reference]]></category>
            <dc:creator><![CDATA[Johann Pardanaud]]></dc:creator>
            <pubDate>Thu, 01 Feb 2018 15:17:35 GMT</pubDate>
            <atom:updated>2020-06-30T12:11:45.065Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GTk4pgFNY9Khu_oO6-6fSg.png" /></figure><p><strong>My new articles are now published on my personal website. Find an updated version of this story at </strong><a href="https://johann.pardanaud.com/blog/about-circular-references-in-php"><strong>https://johann.pardanaud.com/blog/about-circular-references-in-php</strong></a><strong>.</strong></p><p>Let me talk about some issues I encountered with circular references while working on my <a href="https://github.com/nesk/puphpeteer">PuPHPeteer</a> library.</p><h4>How you can end with circular references</h4><p>Under the hood, PuPHPeteer uses <a href="https://github.com/nesk/rialto">Rialto</a>, a library I&#39;ve created for the occasion. Rialto is a library to manage Node resources, therefore it needs to create a Node process and communicate with it. To do so, it has a ProcessSupervisor class which handles everything. In the early stage development, this class was handling way too much features.</p><p>To improve the <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a>, I&#39;ve moved all the functions handling data serialization to two new classes: DataSerializer and DataUnserializer. That way, the ProcessSupervisor class only had to instanciate these two classes and use their public methods.</p><p>However, the DataUnserializer class needed to keep a reference to its ProcessSupervisor parent (so it can transmit the reference to some classes it creates on unserialization). We had this code organization:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OLqbfvMjn8mUicOIB2u8pQ.png" /><figcaption>If anyone told me I would need a UML diagram one day, I would have not believed him.</figcaption></figure><p>And this is where we have an issue: if the root object is destroyed, the ProcessSupervisor and DataUnserializer instances will stay alive because they reference each other.</p><h4>How circular references can be harmful</h4><p>Some languages can handle them pretty well, but since PHP&#39;s garbage collector is using <a href="http://php.net/manual/en/features.gc.refcounting-basics.php">reference</a> <a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Reference_counting">counting</a>, the two instances will be destroyed only once PHP shuts down:</p><blockquote>Fortunately, PHP will clean up this data structure at the end of the request, but before then, this is taking up valuable space in memory.</blockquote><blockquote>— <a href="http://php.net/manual/en/features.gc.refcounting-basics.php#features.gc.cleanup-problems">php.net</a></blockquote><p>However, memory isn&#39;t the only issue here. Let me take another example with Rialto: the ProcessSupervisor uses the __destruct method to kill the Node process once the class is no longer used, and here&#39;s what the PHP documentation states:</p><blockquote>The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.</blockquote><blockquote>— <a href="http://php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor">php.net</a></blockquote><p>This means the Node process will be kept alive until PHP shuts down because there will still exist a reference to the ProcessSupervisor instance.</p><p>So, having a circular dependency in PHP can be harmful for two reasons:</p><ul><li><strong>Memory allocation</strong>: if your instances store large chunks of data (arrays with thousands of very long strings, etc…).</li><li><strong>Destruction</strong>: if your instances needs to do some cleanup on destruction, the cleanup will only happen on PHP shutdown.</li></ul><p>Ultimately, these issues will bother you essentially with long running processes, but even with simple web requests there could some side effects if your classes allocates a high volume of memory or needs to do proper cleaning at the right time.</p><h4>How you can solve circular references</h4><p>You have three ways to solve your circular references issues:</p><ul><li><strong>Use </strong><a href="https://en.wikipedia.org/wiki/Weak_reference"><strong>weak references</strong></a>:</li></ul><blockquote>A <strong>weak reference</strong> is a <a href="https://en.wikipedia.org/wiki/Reference_(computer_science)">reference</a> that does not protect the referenced <a href="https://en.wikipedia.org/wiki/Object_(computer_science)">object</a> from collection by a <a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)">garbage collector</a>.</blockquote><blockquote>— <a href="https://en.wikipedia.org/wiki/Weak_reference">Wikipedia</a></blockquote><p>Weak references is a missing language feature in PHP. You will need to use an extension. You can use either <a href="https://github.com/colder/php-weakref">WeakRef</a> or <a href="https://github.com/pinepain/php-ref">PHP Ref,</a> I will use the first one below.</p><p>Since this solution requires a PHP extension, it&#39;s not the best choice if you&#39;re writing a library or a command line, but if you&#39;re creating a website you will manage by yourself then it&#39;s probably what you should use.</p><p>Here&#39;s a code example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a2e9afcac819a5e19b468efa3f3a35f/href">https://medium.com/media/5a2e9afcac819a5e19b468efa3f3a35f/href</a></iframe><ul><li><strong>Create a dereferencing method</strong>:</li></ul><p>If you cannot install a PHP extension (because of a shared hosting, or whatever…), you can create a method to cleanup your circular references.</p><p>However, this solution requires to call explicitly the dereferencing method, otherwise your circular reference will remain until PHP shuts down.</p><p>Since you&#39;re required to call a method, this might not be the best idea for a library. Here&#39;s an example of the implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0eccb33b199d331e6b588cda635785b/href">https://medium.com/media/f0eccb33b199d331e6b588cda635785b/href</a></iframe><ul><li><strong>Change your code structure</strong>:</li></ul><p>If you don&#39;t want to add a derefencing method because you&#39;re creating a library and you want to keep the API really simple, you might want to change your code structure.</p><p>This is what I did with Rialto. I didn&#39;t want to put back the unserializing methods in the ProcessSupervisor class, so I went with a different solution and replaced the DataUnserializer class by a UnserializesData trait which is imported in the ProcessSupervisor class. Here&#39;s the new code organization:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*dZkNmi9n2QNAJqZvzrpzOw.png" /><figcaption>Probably the last UML diagram I will ever create…</figcaption></figure><p>However, this is not always possible! I was able to do it here because Rialto used only one instance of the DataUnserializer class. If I had used multiple instances of this class (for… reasons), I could not have done that.</p><h4>This could be way better</h4><p>PHP is a real mess when it comes to manage circular references. <a href="https://wiki.php.net/rfc/weakreferences">A RFC was proposed</a> to add a SplWeakRef class to the standard PHP library, but it is marked as inactive…</p><p><strong>Update: </strong><a href="https://wiki.php.net/rfc/weakrefs">Weak references are coming</a> to PHP 7.4!</p><p>In the other hand, not having weak references wouldn&#39;t be an issue if PHP&#39;s garbage collector used <a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Tracing">the tracing algorithm</a>, but instead we have that crappy reference counting…</p><p>If you encounter circular references in your code, two words: good luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10f71f811e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a default error page with Laravel]]></title>
            <link>https://medium.com/@johann.pardanaud/create-a-default-error-page-with-laravel-ac2b0596290b?source=rss-ad0fadfaa0dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac2b0596290b</guid>
            <category><![CDATA[rendering]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[exception]]></category>
            <category><![CDATA[https]]></category>
            <dc:creator><![CDATA[Johann Pardanaud]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 15:29:00 GMT</pubDate>
            <atom:updated>2020-06-30T12:10:50.962Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Y6V7ldrtyNHrdJ7gofDBg.png" /></figure><p><strong>My new articles are now published on my personal website. Find an updated version of this story at </strong><a href="https://johann.pardanaud.com/blog/create-a-default-error-page-with-laravel"><strong>https://johann.pardanaud.com/blog/create-a-default-error-page-with-laravel</strong></a><strong>.</strong></p><p>Defining a <a href="https://laravel.com/docs/5.5/errors#custom-http-error-pages">custom page for HTTP errors</a> is pretty easy with Laravel:</p><blockquote>Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php.</blockquote><p>However, this requires to create a Blade file for each HTTP status you want to handle. An ideal solution would be to create a default view to display a generic error page for unhandled HTTP error codes.</p><p>First, create a resources/views/errors/default.blade.php file with the content you want to warn your users about an unhandled exception. Now, we need to edit the exception handler in app/Exceptions/Handler.php.</p><p>Internally, the exception handler uses <a href="https://github.com/laravel/framework/blob/ff11fd7205431e31837538cc86c8e4dd4917bddb/src/Illuminate/Foundation/Exceptions/Handler.php#L386-L407">the </a><a href="https://github.com/laravel/framework/blob/ff11fd7205431e31837538cc86c8e4dd4917bddb/src/Illuminate/Foundation/Exceptions/Handler.php#L386-L407">renderHttpException() method</a> to convert a HTTP exception to a response. You can see it checks if a view exists for the status code with the following expression: view()-&gt;exists($view = &quot;errors::{$status}&quot;)</p><p>Then, if there is no corresponding view, Laravel will show its default view for the exception. What we want now is to act <em>before</em> the code of the renderHttpException() method is executed, so we can check by ourselves if there is no corresponding view and then use our <em>own</em> default view instead of the one provided by Laravel.</p><p>Copy the declaration of the renderHttpException() method in your exception handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ded53983891f53f05c9925fb38a06e46/href">https://medium.com/media/ded53983891f53f05c9925fb38a06e46/href</a></iframe><p>Remember to import the HttpException class with use Symfony\Component\HttpKernel\Exception\HttpException at the top of the file.</p><p>Now, we need to check the existence of a view for the exception status code. If there is no corresponding view, we return a response with the default.blade.php view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/100a87fad64dd33a844920614c33643b/href">https://medium.com/media/100a87fad64dd33a844920614c33643b/href</a></iframe><p>And that&#39;s it! You may have noticed that we were only talking about HTTP exceptions since the beginning of this article, however <a href="https://github.com/laravel/framework/blob/ff11fd7205431e31837538cc86c8e4dd4917bddb/src/Illuminate/Foundation/Exceptions/Handler.php#L290-L292">Laravel will automatically convert other exceptions</a> to HTTP exceptions (with a 500 status code), thus all exceptions will be handled by your default view. 😉</p><p>If you would like to see Laravel support for default error pages right out of the box, <a href="https://github.com/laravel/framework/pull/22820">you can add a reaction 👍 to my pull-request</a>.</p><p>One last thing since we were talking about error handling in this article. Note that, since Laravel 5.5.5, <a href="https://twitter.com/themsaid/status/910135205989625856">you can use the </a><a href="https://twitter.com/themsaid/status/910135205989625856">Route::fallback() method</a> to display advanced 404 error pages (and you will have access to sessions, cookies, etc…).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac2b0596290b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running Electron on Heroku]]></title>
            <link>https://medium.com/@johann.pardanaud/running-electron-on-heroku-db3866a690a3?source=rss-ad0fadfaa0dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/db3866a690a3</guid>
            <category><![CDATA[headless-browser]]></category>
            <category><![CDATA[xvfb]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[nightmare]]></category>
            <category><![CDATA[electron]]></category>
            <dc:creator><![CDATA[Johann Pardanaud]]></dc:creator>
            <pubDate>Mon, 06 Mar 2017 15:12:46 GMT</pubDate>
            <atom:updated>2020-06-30T12:09:44.389Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EFB9WgDEGgOktdtv_oQDOA.png" /></figure><p><strong>My new articles are now published on my personal website. Find an updated version of this story at </strong><a href="https://johann.pardanaud.com/blog/running-electron-on-heroku"><strong>https://johann.pardanaud.com/blog/running-electron-on-heroku</strong></a><strong>.</strong></p><blockquote>This article was written when only <a href="https://devcenter.heroku.com/articles/cedar-14-stack">the </a><a href="https://devcenter.heroku.com/articles/cedar-14-stack">Cedar-14 stack</a> was available. I didn&#39;t try to run Electron on <a href="https://devcenter.heroku.com/articles/heroku-16-stack">the new </a><a href="https://devcenter.heroku.com/articles/heroku-16-stack">Heroku-16 stack</a>, I will update the article the day I migrate my own app.</blockquote><p>These last days I&#39;ve developed a module to scrap some data with <a href="http://www.nightmarejs.org/">Nightmare</a>, a headless browser based on <a href="https://electron.atom.io/">Electron</a>. Once my code was finished I tried to run it on <a href="https://www.heroku.com/home">Heroku&#39;s infrastructure</a> and was pretty disappointed to see it wasn&#39;t running though it worked perfectly on my local machine.</p><p>After a bit of research, I&#39;ve found that “<a href="https://github.com/electron/electron/blob/e315116/docs/tutorial/testing-on-headless-ci.md">Electron requires a display driver to function</a>”. Of course, Heroku doesn&#39;t provide any display driver since there is nearly no incentive to that. Thankfully, the Electron team addressed this issue (on the same documentation page) and recommends to use <a href="https://www.x.org/archive/X11R7.6/doc/man/man1/Xvfb.1.xhtml">Xvfb</a>, a tool which allows to run a virtual display server on your machine.</p><h4>Installing Xvfb</h4><p>You will need to add the <a href="https://github.com/heroku/heroku-buildpack-apt">APT buildpack</a> to your app, which allows to install APT packages <a href="https://devcenter.heroku.com/articles/slug-compiler">at compile time</a>:</p><pre>heroku buildpacks:add -i 1 <a href="https://github.com/heroku/heroku-buildpack-apt">https://github.com/heroku/heroku-buildpack-apt</a></pre><p>Then add a file named Aptfile in the root directory of your app with the following contents, each line contains the name of a package we want to install:</p><pre>libxss1<br>xvfb</pre><p>The next time you will deploy your application, Xvfb will be installed on your dynos, however the command will not work right out of the box because of the architecture of Heroku&#39;s servers. You will need to install the <a href="https://github.com/captain401/heroku-buildpack-xvfb">Xvfb buildpack</a> written by <a href="https://github.com/yoz">yoz</a>, which patches the Xvfb command so it can run properly. Obviously, this buildpack should run <em>after</em> the APT one:</p><pre>heroku buildpacks:add -i 2 <a href="https://github.com/captain401/heroku-buildpack-xvfb">https://github.com/captain401/heroku-buildpack-xvfb</a></pre><h4>Running the virtual display server</h4><p>Now you need to run Xvfb before launching your Electron process. Instead of starting a new instance of Xvfb each time you want to run Electron, you could launch Xvfb at startup. Heroku provides the ability to run a bash script on startup <a href="https://devcenter.heroku.com/articles/dynos#the-profile-file">through a </a><a href="https://devcenter.heroku.com/articles/dynos#the-profile-file">.profile file</a> located at the root directory of your app. Here is what it should contain:</p><pre>export DISPLAY=&#39;:99.0&#39;<br>Xvfb :99 -screen 0 1024x768x24 &gt; /dev/null 2&gt;&amp;1 &amp;</pre><p>These two lines are copied from <a href="https://github.com/electron/electron/blob/e315116/docs/tutorial/testing-on-headless-ci.md#travis-ci">the configuration the Electron team provides for Travis CI</a>. The $DISPLAY variable identifies your Xvfb instance and will be read by Electron, according to the documentation:</p><blockquote>Chromium in Electron will automatically look for $DISPLAY, so no further configuration of your app is required.</blockquote><p>In the second line we run the Xvfb server in background and define the screen properties.</p><p>One last thing, maybe you want to run the virtual display server only on one type of dyno. Heroku provides <a href="https://devcenter.heroku.com/articles/dynos#local-environment-variables">a </a><a href="https://devcenter.heroku.com/articles/dynos#local-environment-variables">$DYNO variable</a> containing the process type and an identifier. So if you want to run Xvfb only for the scraper process type, you will need to write the following code in your .profile file:</p><pre>if [ -z &quot;$DYNO&quot; ] || [[ $DYNO == *&quot;scraper&quot;* ]]; then<br>    export DISPLAY=&#39;:99.0&#39;<br>    Xvfb :99 -screen 0 1024x768x24 &gt; /dev/null 2&gt;&amp;1 &amp;<br>fi</pre><p>The [ -z &quot;$DYNO&quot;] part checks if the variable is unset or empty. I prefer to add this since Heroku states in its documentation:</p><blockquote>The $DYNO variable is experimental and subject to change or removal.</blockquote><p>Thus, if the $DYNO variable gets removed, your virtual display server will still be running, but on all your dynos instead on the type you defined.</p><h4>It might be simpler in the future</h4><p>Since December, Chromium supports <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=546953#c148">the </a><a href="https://bugs.chromium.org/p/chromium/issues/detail?id=546953#c148">--headless command line flag</a> which allows to run it without any display server. I don&#39;t know if Electron will integrate this feature in the future, but this could be great for saving time for developers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db3866a690a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>