Skip to content

Instantly share code, notes, and snippets.

View diogomachado's full-sized avatar
🎯
Focusing

Diogo Machado diogomachado

🎯
Focusing
View GitHub Profile

Semantic Commit Messages

See how a minor change to your commit message style can make you a better programmer.

Format: <type>(<scope>): <subject>

<scope> is optional

Example

@diogomachado
diogomachado / error-handling-with-fetch.md
Created January 17, 2021 15:18 — forked from odewahn/error-handling-with-fetch.md
Processing errors with Fetch API

I really liked @tjvantoll article Handling Failed HTTP Responses With fetch(). The one thing I found annoying with it, though, is that response.statusText always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body.

Here's a modification that will capture this message. The key is that rather than throwing an error, you just throw the response and then process it in the catch block to extract the message in the body:

fetch("/api/foo")
  .then( response => {
    if (!response.ok) { throw response }
    return response.json()  //we only get here if there is no error
 })
@diogomachado
diogomachado / mysql-docker.sh
Created January 15, 2021 01:39 — forked from spalladino/mysql-docker.sh
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
$("#search").keyup(function(){
_this = this;
// Mostrando apenas as linhas que combinam e escondendo o restante
$.each($("#table tbody tr"), function() {
if($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) === -1)
$(this).hide();
else
$(this).show();
});
});
<?php
/**
* Classe que contem os métodos que iram
* filtrar as entradas enviadas via GET e POST
*
* @filesource
* @author Pedro Elsner <pedro.elsner@gmail.com>
* @license http://creativecommons.org/licenses/by/3.0/br/ Creative Commons 3.0
* @abstract
* @version 1.0
// Somewhere in your controllers for this given example
// Example functions
$scope.itemOnLongPress = function(id) {
console.log('Long press');
}
$scope.itemOnTouchEnd = function(id) {
console.log('Touch end');
}